home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 October / Chip Ekim 2003.iso / prog / tamsurum / tocom / Security Updates / SQL-MSDE-CriticalUpdate_ENU.msi / Hotfix3 / Files / replcom.sql < prev    next >
Encoding:
Text File  |  2001-10-03  |  427.1 KB  |  13,124 lines

  1. /*
  2. ** replcom.sql            1997/02/12 22:03
  3. **
  4. **
  5. ** Copyright Microsoft, Inc. 1998-2000
  6. ** All Rights Reserved.
  7. */
  8.  
  9.  
  10. dump tran master with no_log
  11. go
  12.  
  13. exec dbo.sp_configure 'update',1
  14. go
  15. reconfigure with override
  16. go
  17.  
  18. set ANSI_NULLS off
  19. go
  20.  
  21. use master
  22. go
  23.  
  24. exec dbo.sp_MS_upd_sysobj_category 1 --Capture time for use at the end
  25. go
  26.  
  27.  
  28. dump tran master with no_log
  29. GO
  30.  
  31. /* 
  32. ** Drop the stored procedures in this script using the old dropping SP 
  33. ** and then drop itself
  34. */
  35. if exists (select * from sysobjects
  36.     where type = 'P'
  37.             and name = 'sp_MSdrop_replcom')
  38. begin
  39.     drop procedure sp_MSdrop_replcom
  40. end
  41.  
  42. /*
  43. ** Create stored procedures to drop the stored procedures
  44. ** created by this script
  45. */
  46.  
  47. raiserror('Creating procedure sp_MSdrop_replcom', 0,1)
  48. GO
  49.  
  50. create procedure sp_MSdrop_replcom
  51. as
  52.     if exists (select * from sysobjects 
  53.         where type = 'P'
  54.             and name = 'sp_MScreate_distributor_tables')
  55.         drop procedure sp_MScreate_distributor_tables
  56.  
  57.     if exists (select * from sysobjects
  58.         where type = 'P'
  59.                 and name = 'sp_MSIfExistsRemoteLogin')
  60.         drop procedure sp_MSIfExistsRemoteLogin
  61.  
  62.     if exists (select * from sysobjects
  63.         where type = 'P'
  64.                 and name = 'sp_helppublicationsync')
  65.         drop procedure sp_helppublicationsync
  66.  
  67.     if exists (select * from sysobjects
  68.             where type = 'P'
  69.                 and name = 'sp_addpublication_snapshot')
  70.         drop procedure sp_addpublication_snapshot
  71.  
  72.     if exists (select * from sysobjects
  73.             where type = 'P'
  74.                 and name = 'sp_MShelpobjectpublications')
  75.         drop procedure sp_MShelpobjectpublications
  76.  
  77.     if exists (select * from sysobjects
  78.             where type = 'P'
  79.                 and name = 'sp_addpublisher')
  80.         drop procedure sp_addpublisher
  81.  
  82.     if exists (select * from sysobjects
  83.             where type = 'P'
  84.                 and name = 'sp_addsubscriber')
  85.         drop procedure sp_addsubscriber
  86.  
  87.     if exists (select * from sysobjects
  88.             where type = 'P'
  89.                 and name = 'sp_addsubscriber_schedule')
  90.         drop procedure sp_addsubscriber_schedule
  91.  
  92.     if exists (select * from sysobjects
  93.             where type = 'P'
  94.                 and name = 'sp_changesubscriber_schedule')
  95.         drop procedure sp_changesubscriber_schedule
  96.  
  97.     if exists (select * from sysobjects
  98.             where type = 'P'
  99.                 and name = 'sp_changesubscriber')
  100.         drop procedure sp_changesubscriber
  101.  
  102.     IF EXISTS (SELECT * FROM sysobjects
  103.             WHERE type = 'P'
  104.                 AND name = 'sp_MScreate_dist_tables')
  105.         DROP PROCEDURE sp_MScreate_dist_tables
  106.  
  107.     IF EXISTS (SELECT * FROM sysobjects
  108.             WHERE type = 'P'
  109.                 AND name = 'sp_MSupdate_mqserver_distdb')
  110.         DROP PROCEDURE sp_MSupdate_mqserver_distdb
  111.  
  112.     if exists (select * from sysobjects
  113.             where type = 'P'
  114.                 and name = 'sp_distcounters')
  115.         drop procedure sp_distcounters
  116.  
  117.  
  118.     if exists (select * from sysobjects
  119.             where type = 'P'
  120.                 and name = 'sp_droppublisher')
  121.         drop procedure sp_droppublisher
  122.  
  123.     if exists (select * from sysobjects
  124.             where type = 'P'
  125.                 and name = 'sp_dropsubscriber')
  126.         drop procedure sp_dropsubscriber
  127.  
  128.     if exists (select * from sysobjects
  129.             where type = 'P'
  130.                 and name = 'sp_dsninfo')
  131.         drop procedure sp_dsninfo
  132.  
  133.     if exists (select * from sysobjects
  134.             where type = 'P'
  135.                 and name = 'sp_enumdsn')
  136.         drop procedure sp_enumdsn
  137.  
  138.     if exists (select * from sysobjects
  139.             where type = 'P'
  140.                 and name = 'sp_helpdistributor')
  141.         drop procedure sp_helpdistributor
  142.  
  143.     if exists (select * from sysobjects
  144.             where type = 'P'
  145.                 and name = 'sp_helppublicationsync')    /* Remove old version */
  146.         drop procedure sp_helppublicationsync
  147.  
  148.     if exists (select * from sysobjects
  149.             where type = 'P'
  150.                 and name = 'sp_helpreplicationdb')
  151.         drop procedure sp_helpreplicationdb
  152.  
  153.     if exists (select * from sysobjects
  154.             where type = 'P'
  155.                 and name = 'sp_helpsubscriberinfo')
  156.         drop procedure sp_helpsubscriberinfo
  157.  
  158.     if exists (select * from sysobjects
  159.             where type = 'P'
  160.                 and name = 'sp_publishdb')
  161.         drop procedure sp_publishdb
  162.  
  163.     if exists (select * from sysobjects
  164.             where type = 'P'
  165.                 and name = 'sp_replica')
  166.         drop procedure sp_replica
  167.  
  168.     if exists (select * from sysobjects
  169.             where type = 'P'
  170.                 and name = 'sp_adddistributiondb')
  171.         drop procedure sp_adddistributiondb
  172.  
  173.     if exists (select * from sysobjects
  174.             where type = 'P'
  175.                 and name = 'sp_changedistributiondb')
  176.         drop procedure sp_changedistributiondb
  177.  
  178.     if exists (select * from sysobjects
  179.             where type = 'P'
  180.                 and name = 'sp_helpdistributiondb')
  181.         drop procedure sp_helpdistributiondb
  182.  
  183.     if exists (select * from sysobjects
  184.             where type = 'P'
  185.                 and name = 'sp_dropdistributiondb')
  186.         drop procedure sp_dropdistributiondb
  187.  
  188.     if exists (select * from sysobjects
  189.             where type = 'P'
  190.                 and name = 'sp_adddistpublisher')
  191.         drop procedure sp_adddistpublisher
  192.  
  193.     if exists (select * from sysobjects
  194.             where type = 'P'
  195.                 and name = 'sp_changedistpublisher')
  196.         drop procedure sp_changedistpublisher
  197.  
  198.     if exists (select * from sysobjects
  199.             where type = 'P'
  200.                 and name = 'sp_helpdistpublisher')
  201.         drop procedure sp_helpdistpublisher
  202.  
  203.     if exists (select * from sysobjects
  204.             where type = 'P'
  205.                 and name = 'sp_dropdistpublisher')
  206.         drop procedure sp_dropdistpublisher
  207.  
  208.     if exists (select * from sysobjects
  209.             where type = 'P'
  210.                 and name = 'sp_MSadd_distributor_alerts_and_responses')
  211.         drop procedure  sp_MSadd_distributor_alerts_and_responses
  212.  
  213.     if exists (select * from sysobjects
  214.             where type = 'P'
  215.                 and name = 'sp_MSdrop_distributor_alerts_and_responses')
  216.         drop procedure  sp_MSdrop_distributor_alerts_and_responses
  217.  
  218.     if exists (select * from sysobjects
  219.             where type = 'P'
  220.                 and name = 'sp_adddistributor')
  221.         drop procedure  sp_adddistributor
  222.  
  223.     if exists (select * from sysobjects
  224.             where type = 'P'
  225.                 and name = 'sp_dropdistributor')
  226.         drop procedure sp_dropdistributor
  227.  
  228.     if exists (select * from sysobjects
  229.             where type = 'P'
  230.                 and name = 'sp_changedistributor_property')
  231.         drop procedure sp_changedistributor_property
  232.  
  233.     if exists (select * from sysobjects
  234.             where type = 'P'
  235.                 and name = 'sp_helpdistributor_properties')
  236.         drop procedure sp_helpdistributor_properties
  237.  
  238.     if exists (select * from sysobjects
  239.             where type = 'X'
  240.                 and name = 'sp_repldone')
  241.         exec dbo.sp_dropextendedproc 'sp_repldone'
  242.  
  243.     if exists (select * from sysobjects
  244.             where type = 'X'
  245.                 and name = 'sp_repltrans')
  246.         exec dbo.sp_dropextendedproc 'sp_repltrans'
  247.  
  248.     if exists (select * from sysobjects
  249.             where type = 'X'
  250.                 and name = 'sp_replcmds')
  251.         exec dbo.sp_dropextendedproc 'sp_replcmds'
  252.  
  253.     if exists (select * from sysobjects
  254.             where type = 'X'
  255.                 and name = 'sp_replcounters')
  256.         exec dbo.sp_dropextendedproc 'sp_replcounters'
  257.  
  258.     if exists (select * from sysobjects
  259.             where type = 'X'
  260.                 and name = 'sp_replflush')
  261.         exec dbo.sp_dropextendedproc 'sp_replflush'
  262.  
  263.     if exists (select * from sysobjects
  264.             where type = 'X'
  265.                  and name = 'sp_replpostcmd' )
  266.         exec dbo.sp_dropextendedproc 'sp_replpostcmd'
  267.  
  268.     if exists (select * from sysobjects
  269.             where type = 'X'
  270.                  and name = 'sp_replpostschema' )
  271.         exec dbo.sp_dropextendedproc 'sp_replpostschema'
  272.  
  273.     if exists (select * from sysobjects
  274.             where type = 'X'
  275.                  and name = 'sp_replincrementlsn' )
  276.         exec dbo.sp_dropextendedproc 'sp_replincrementlsn'
  277.  
  278.     if exists (select * from sysobjects
  279.             where type = 'X'
  280.                  and name = 'sp_replupdateschema' )
  281.         exec dbo.sp_dropextendedproc 'sp_replupdateschema'
  282.  
  283.     if exists (select * from sysobjects
  284.             where type = 'X'
  285.                  and name = 'sp_replsetoriginator' )
  286.         exec dbo.sp_dropextendedproc 'sp_replsetoriginator'
  287.  
  288.     if exists (select * from sysobjects
  289.             where type = 'X'
  290.                  and name = 'sp_replsetsyncstatus' )
  291.         exec dbo.sp_dropextendedproc 'sp_replsetsyncstatus'
  292.  
  293.     if exists (select * from sysobjects
  294.             where type = 'X'
  295.                  and name = 'sp_replpostsyncstatus' )
  296.         exec dbo.sp_dropextendedproc 'sp_replpostsyncstatus'
  297.  
  298.     if exists (select * from sysobjects
  299.             where type = 'X'
  300.                 and name = 'xp_enumdsn')
  301.         exec dbo.sp_dropextendedproc 'xp_enumdsn'
  302.  
  303.     if exists (select * from sysobjects
  304.             where type = 'X'
  305.                 and name = 'xp_oledbinfo')
  306.         exec dbo.sp_dropextendedproc 'xp_oledbinfo'
  307.  
  308.     if exists (select * from sysobjects
  309.             where type = 'X'
  310.                 and name = 'xp_dsninfo')
  311.         exec dbo.sp_dropextendedproc 'xp_dsninfo'
  312.  
  313.     if exists (select * from sysobjects
  314.             where type = 'X'
  315.                 and name = 'xp_repl_encrypt')
  316.         exec dbo.sp_dropextendedproc 'xp_repl_encrypt'
  317.  
  318.     if exists (select * from sysobjects
  319.             where type = 'X'
  320.                 and name = 'xp_repl_convert_encrypt')
  321.         exec dbo.sp_dropextendedproc 'xp_repl_convert_encrypt'
  322.  
  323.     if exists (select * from sysobjects
  324.             where type = 'X'
  325.                 and name = 'xp_repl_help_connect')
  326.         exec dbo.sp_dropextendedproc 'xp_repl_help_connect'
  327.  
  328.      if exists (select * from sysobjects
  329.             where type = 'X'
  330.                 and name = 'xp_replproberemsrv')
  331.         exec dbo.sp_dropextendedproc 'xp_replproberemsrv'
  332.  
  333.     -- sp_helpsubscriber is removed permanently from the system.
  334.     if exists (select * from sysobjects
  335.         where type = 'P'
  336.                 and name = 'sp_helpsubscriber')
  337.         drop procedure sp_helpsubscriber
  338.  
  339.     -- sp_MSrepl_encrypt obsolete; use xp_repl_encrypt
  340.     if exists (select * from sysobjects
  341.         where type = 'P'
  342.                 and name = 'sp_MSrepl_encrypt')
  343.         drop procedure sp_MSrepl_encrypt
  344.  
  345.     if exists (select * from sysobjects 
  346.         where name = 'sp_add_agent_profile' 
  347.                 and type = 'P')
  348.         drop procedure sp_add_agent_profile
  349.  
  350.     if exists (select * from sysobjects 
  351.         where name = 'sp_help_agent_profile' 
  352.                 and type = 'P')
  353.         drop procedure sp_help_agent_profile
  354.  
  355.     if exists (select * from sysobjects 
  356.         where name = 'sp_help_agent_default' 
  357.             and type = 'P')
  358.         drop procedure sp_help_agent_default
  359.  
  360.     if exists (select * from sysobjects 
  361.         where name = 'sp_drop_agent_profile' 
  362.                 and type = 'P')
  363.         drop procedure sp_drop_agent_profile
  364.  
  365.     if exists (select name from sysobjects 
  366.         where name = 'sp_MSupdate_agenttype_default'
  367.                 and type = 'P')
  368.         drop procedure sp_MSupdate_agenttype_default
  369.  
  370.     if exists (select * from sysobjects 
  371.         where name = 'sp_MSvalidate_agent_parameter' 
  372.                 and type = 'P')
  373.         drop procedure sp_MSvalidate_agent_parameter
  374.  
  375.     if exists (select * from sysobjects 
  376.         where name = 'sp_add_agent_parameter' 
  377.                 and type = 'P')
  378.         drop procedure sp_add_agent_parameter
  379.  
  380.     if exists (select * from sysobjects 
  381.         where name = 'sp_generate_agent_parameter' 
  382.                 and type = 'P')
  383.         drop procedure sp_generate_agent_parameter
  384.  
  385.     if exists (select * from sysobjects 
  386.         where name = 'sp_change_agent_parameter' 
  387.                 and type = 'P')
  388.         drop procedure sp_change_agent_parameter
  389.  
  390.     if exists (select * from sysobjects 
  391.         where name = 'sp_change_agent_profile' 
  392.                 and type = 'P')
  393.         drop procedure sp_change_agent_profile
  394.  
  395.     if exists (select * from sysobjects 
  396.         where name = 'sp_help_agent_parameter' 
  397.                 and type = 'P')
  398.       drop procedure sp_help_agent_parameter
  399.  
  400.     if exists (select * from sysobjects 
  401.         where name = 'sp_drop_agent_parameter' 
  402.                 and type = 'P')
  403.       drop procedure sp_drop_agent_parameter
  404.  
  405.     if exists (select * from sysobjects 
  406.         where name = 'sp_MShelp_distdb' 
  407.                 and type = 'P')
  408.       drop procedure sp_MShelp_distdb
  409.  
  410.     if exists (select * from sysobjects 
  411.         where name = 'sp_MShelp_distdb' 
  412.                 and type = 'P')
  413.       drop procedure sp_MShelp_distdb
  414.  
  415.     if exists (select * from sysobjects 
  416.         where name = 'sp_MSenum_misc_agents'
  417.                 and type = 'P')
  418.       drop procedure sp_MSenum_misc_agents
  419.  
  420.     if exists (select * from sysobjects 
  421.         where name = 'sp_MSupdate_replication_status' 
  422.                 and type = 'P')
  423.       drop procedure sp_MSupdate_replication_status
  424.  
  425.     if exists (select * from sysobjects 
  426.         where name = 'sp_MSload_replication_status' 
  427.                 and type = 'P')
  428.       drop procedure sp_MSload_replication_status
  429.  
  430.     if exists (select * from sysobjects 
  431.         where name = 'sp_MScreate_replication_status_table' 
  432.                 and type = 'P')
  433.       drop procedure sp_MScreate_replication_status_table
  434.  
  435.     if exists (select * from sysobjects 
  436.         where name = 'sp_MShelp_replication_status' 
  437.                 and type = 'P')
  438.       drop procedure sp_MShelp_replication_status
  439.  
  440.     if exists (select * from sysobjects 
  441.         where name = 'sp_MSenum_replication_agents' 
  442.                 and type = 'P')
  443.       drop procedure sp_MSenum_replication_agents
  444.  
  445.     if exists (select * from sysobjects 
  446.         where name = 'sp_replication_agent_checkup' 
  447.                 and type = 'P')
  448.       drop procedure sp_replication_agent_checkup
  449.  
  450.     if exists (select * from sysobjects 
  451.         where name = 'sp_MSreplrole' 
  452.                 and type = 'P')
  453.       drop procedure sp_MSreplrole
  454.  
  455.     if exists (select * from sysobjects 
  456.         where name = 'sp_MScreate_replication_checkup_agent' 
  457.                 and type = 'P')
  458.       drop procedure sp_MScreate_replication_checkup_agent
  459.  
  460.     if exists (select * from sysobjects 
  461.         where name = 'sp_MSenum_replication_job' 
  462.                 and type = 'P')
  463.       drop procedure sp_MSenum_replication_job
  464.  
  465.     if exists (select * from sysobjects 
  466.         where name = 'sp_MSrepl_dbrole' 
  467.                 and type = 'P')
  468.       drop procedure sp_MSrepl_dbrole
  469.  
  470.     if exists (select * from sysobjects
  471.         where type = 'P'
  472.                 and name = 'sp_oledbinfo')
  473.         drop procedure sp_oledbinfo
  474.  
  475.     if exists (select * from sysobjects
  476.         where type = 'P'
  477.                 and name = 'sp_enumoledbdatasources')
  478.         drop procedure sp_enumoledbdatasources
  479.  
  480.     if exists (select * from sysobjects
  481.         where type = 'P'
  482.                 and name = 'sp_MSget_oledbinfo')
  483.         drop procedure sp_MSget_oledbinfo
  484.  
  485.     if exists (select * from sysobjects 
  486.         where name = 'sp_changedistributor_password' 
  487.                 and type = 'P')
  488.       drop procedure sp_changedistributor_password
  489.  
  490.     if exists (select * from sysobjects 
  491.         where name = 'sp_grant_publication_access' 
  492.                 and type = 'P')
  493.       drop procedure sp_grant_publication_access
  494.  
  495.     if exists (select * from sysobjects 
  496.         where name = 'sp_revoke_publication_access' 
  497.                 and type = 'P')
  498.       drop procedure sp_revoke_publication_access
  499.  
  500.     if exists (select * from sysobjects 
  501.         where name = 'sp_help_publication_access' 
  502.                 and type = 'P')
  503.       drop procedure sp_help_publication_access
  504.  
  505.     if exists (select * from sysobjects 
  506.         where name = 'sp_check_publication_access' 
  507.                 and type = 'P')
  508.       drop procedure sp_check_publication_access
  509.  
  510.     if exists (select * from sysobjects 
  511.         where name = 'sp_MSinit_replication_perfmon' 
  512.                 and type = 'P')
  513.       drop procedure sp_MSinit_replication_perfmon
  514.  
  515.     if exists (select * from sysobjects 
  516.         where name = 'sp_MSrepl_startup' 
  517.                 and type = 'P')
  518.       drop procedure sp_MSrepl_startup
  519.  
  520.     if exists (select * from sysobjects 
  521.         where name = 'sp_MSflush_access_cache' 
  522.                 and type = 'P')
  523.       drop procedure sp_MSflush_access_cache
  524.  
  525.     if exists (select * from sysobjects 
  526.         where name = 'sp_MSreinit_failed_subscriptions' 
  527.                 and type = 'P')
  528.       drop procedure sp_MSreinit_failed_subscriptions
  529.  
  530.     if exists (select * from sysobjects 
  531.         where name = 'sp_add_datatype_mapping' 
  532.                 and type = 'P')
  533.         drop procedure sp_add_datatype_mapping
  534.     
  535.     if exists (select * from sysobjects 
  536.         where name = 'sp_help_datatype_mapping' 
  537.                 and type = 'P')
  538.         drop procedure sp_help_datatype_mapping
  539.  
  540.     if exists (select * from sysobjects 
  541.         where name = 'sp_MSrepl_gettype_mappings' 
  542.                 and type = 'P')
  543.         drop procedure sp_MSrepl_gettype_mappings
  544.         
  545.     if exists (select * from sysobjects 
  546.         where name = 'sp_MSfix_6x_tasks' 
  547.                 and type = 'P')
  548.         drop procedure sp_MSfix_6x_tasks
  549.  
  550.     if exists (select * from sysobjects 
  551.         where name = 'sp_MSget_agent_names' 
  552.                 and type = 'P')
  553.         drop procedure sp_MSget_agent_names
  554.  
  555.     -- Common conflict viewer support procs
  556.     if exists (select * from sysobjects
  557.             where type = 'P'
  558.                 and name = 'sp_MShelpconflictpublications')
  559.         drop procedure sp_MShelpconflictpublications
  560.  
  561.     -- Procs for managing dynamic snapshot views
  562.     if exists (select * from sysobjects
  563.             where type = 'P'
  564.                 and name = 'sp_MScleanupdynsnapshotvws')
  565.         drop procedure sp_MScleanupdynsnapshotvws
  566.  
  567.     if exists (select * from sysobjects
  568.             where type = 'P'
  569.                 and name = 'sp_MScleanupmergepublisher')
  570.         drop procedure sp_MScleanupmergepublisher
  571.  
  572.     if exists (select * from sysobjects
  573.             where type = 'P'
  574.                 and name = 'sp_MScleanupmergepublisherdb')
  575.         drop procedure sp_MScleanupmergepublisherdb
  576.     
  577.     if exists (select * from sysobjects
  578.             where type = 'P'
  579.                 and name = 'sp_MShelp_replication_table')
  580.         drop procedure sp_MShelp_replication_table
  581.     if exists (select * from sysobjects
  582.             where type = 'P'
  583.                 and name = 'sp_MScopyscriptfile')
  584.         drop procedure sp_MScopyscriptfile
  585.  
  586.     if exists (select * from sysobjects
  587.             where type = 'FN'
  588.                 and name = 'fn_replgetagentcommandlinefromjobid')
  589.         drop function system_function_schema.fn_replgetagentcommandlinefromjobid
  590.     
  591.     if exists (select * from sysobjects
  592.             where type = 'P'
  593.                 and name = 'sp_replproberemoteserver')
  594.         drop procedure sp_replproberemoteserver
  595.  
  596.     if exists (select * from sysobjects
  597.             where type = 'FN'
  598.                 and name = 'fn_getpersistedservernamecasevariation'
  599.                 and user_id('system_function_schema') = uid)
  600.         drop function system_function_schema.fn_getpersistedservernamecasevariation
  601.  
  602. GO
  603.  
  604. /* 
  605. **  We must execute dbo.sp_MSdrop_replcom here since sp_MSdrop_replcom may not exist
  606. **  before this script is applied and it is possible
  607. **  that some replication sps are left. (We ignore error when dropping
  608. **  the replication so it is possible sp_MSdrop_replcom is dropped but
  609. **  some replication sps were left).
  610. */
  611. exec dbo.sp_MSdrop_replcom
  612. go
  613.  
  614. raiserror('Creating procedure sp_MScreate_distributor_tables', 0,1)
  615. GO
  616.  
  617. create procedure sp_MScreate_distributor_tables
  618. as
  619.     declare @profile_id     int
  620.     declare @retcode    int
  621.     declare @profile_name nvarchar(100)
  622.     declare @profile_desc nvarchar(100)
  623.  
  624.     /* Create MSpublishers table */
  625.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSdistpublishers'
  626.         and xtype = 'U')
  627.     begin
  628.         DROP TABLE msdb..MSdistpublishers
  629.         if @@error<> 0 goto FAILURE
  630.     end
  631.  
  632.     /* Create MSdistributiondbs table */
  633.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSdistributiondbs'
  634.         and xtype = 'U')
  635.     begin
  636.         DROP TABLE msdb..MSdistributiondbs
  637.         if @@error<> 0 goto FAILURE
  638.     end
  639.  
  640.     /* create MSdistributor table */
  641.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSdistributor'
  642.         and xtype = 'U')
  643.     begin
  644.         DROP TABLE msdb..MSdistributor
  645.         if @@error<> 0 goto FAILURE
  646.     end
  647.  
  648.     /* create sysreplicationalerts table */
  649.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'sysreplicationalerts'
  650.         and xtype = 'U')
  651.     begin
  652.         DROP TABLE msdb..sysreplicationalerts
  653.         if @@error<> 0 goto FAILURE
  654.     end
  655.     
  656.     /* create MSagent_profiles table */
  657.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSagent_profiles'
  658.         and xtype = 'U')
  659.     begin
  660.         DROP TABLE msdb..MSagent_profiles
  661.         if @@error<> 0 goto FAILURE
  662.     end
  663.  
  664.     /* create MSagent_parameters table */
  665.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSagent_parameters' 
  666.         and xtype = 'U')
  667.     begin
  668.         DROP TABLE msdb..MSagent_parameters
  669.         if @@error<> 0 goto FAILURE
  670.     end
  671.  
  672.     /* create MSdatatype_mappings table */
  673.     IF EXISTS (SELECT * FROM msdb..sysobjects WHERE name = 'MSdatatype_mappings' 
  674.         and xtype = 'U')
  675.     begin
  676.         DROP TABLE msdb..MSdatatype_mappings
  677.         if @@error<> 0 goto FAILURE
  678.     end
  679.  
  680.     
  681.     CREATE TABLE msdb.dbo.MSdistpublishers
  682.     (
  683.         name                sysname     NOT NULL,
  684.         distribution_db     sysname     NOT NULL,
  685.         working_directory   nvarchar(255) NOT NULL,
  686.         security_mode       int         NOT NULL,
  687.         login               sysname     NOT NULL,
  688.         password            nvarchar(524) NULL,
  689.         active              bit         NOT NULL,
  690.         trusted             bit         NOT NULL,
  691.         thirdparty_flag     bit         NOT NULL
  692.     )
  693.     if @@error<> 0 goto FAILURE
  694.  
  695.     CREATE UNIQUE CLUSTERED INDEX uc1MSdistpublishers 
  696.         ON msdb.dbo.MSdistpublishers(name)
  697.     if @@error<> 0 goto FAILURE
  698.  
  699.  
  700.     CREATE TABLE msdb.dbo.MSdistributiondbs
  701.     (
  702.         name            sysname     NOT NULL,
  703.         min_distretention   int     NOT NULL,
  704.         max_distretention   int     NOT NULL,
  705.         history_retention   int     NOT NULL
  706.     )
  707.     if @@error<> 0 goto FAILURE
  708.  
  709.     CREATE UNIQUE CLUSTERED INDEX uc1MSdistributiondbs 
  710.             ON msdb.dbo.MSdistributiondbs(name)
  711.     if @@error<> 0 goto FAILURE
  712.  
  713.  
  714.  
  715.     CREATE TABLE msdb.dbo.MSdistributor 
  716.     ( 
  717.         property        sysname     NOT NULL, 
  718.         value           nvarchar(3000)  NULL
  719.     )
  720.     if @@error<> 0 goto FAILURE
  721.     
  722.     CREATE UNIQUE CLUSTERED INDEX uc1MSdistributor 
  723.             ON msdb.dbo.MSdistributor(property)
  724.     if @@error<> 0 goto FAILURE
  725.  
  726.     CREATE TABLE msdb.dbo.sysreplicationalerts
  727.     (
  728.         alert_id            int identity(1,1) NOT NULL,
  729.         status              int NOT NULL,
  730.         agent_type          int NULL,
  731.         agent_id            int NULL,
  732.         error_id            int NULL,
  733.         alert_error_code    int NULL,
  734.         time                datetime NOT NULL,
  735.         publisher           sysname NULL,
  736.         publisher_db        sysname NULL,
  737.         publication         sysname NULL,
  738.         publication_type    int NULL,
  739.         subscriber          sysname NULL,
  740.         subscriber_db       sysname NULL,
  741.         article             sysname NULL,
  742.         destination_object  sysname NULL,
  743.         source_object       sysname NULL,
  744.         alert_error_text    ntext NULL
  745.     )
  746.     if @@error<> 0 goto FAILURE
  747.  
  748.     CREATE UNIQUE CLUSTERED INDEX ucsysreplicationalerts ON msdb.dbo.sysreplicationalerts(alert_id)
  749.     if @@error<> 0 goto FAILURE
  750.  
  751.     CREATE TABLE msdb.dbo.MSagent_profiles
  752.     (
  753.         profile_id    int           NOT NULL IDENTITY,
  754.         profile_name  sysname       NOT NULL,
  755.         agent_type          int             NOT NULL, -- 1-Snapshot, 2-Logreader,
  756.                                                       -- 3-Distribution, 4-Merge,
  757.                                                       -- 9-Qreader
  758.         type                int             NOT NULL, -- 0-System, 1-Custom
  759.         description         nvarchar(3000)  NULL,
  760.         def_profile   bit               NOT NULL
  761.     )
  762.     if @@error<> 0 goto FAILURE
  763.  
  764.  
  765.     CREATE UNIQUE CLUSTERED INDEX ucMSagent_profiles ON msdb.dbo.MSagent_profiles
  766.         (profile_name, profile_id, agent_type)
  767.     if @@error<> 0 goto FAILURE
  768.  
  769.     CREATE TABLE msdb.dbo.MSagent_parameters
  770.     (
  771.         profile_id     int          NOT NULL,
  772.         parameter_name       sysname        NOT NULL,
  773.         value                nvarchar(255)  NOT NULL
  774.     )
  775.     if @@error<> 0 goto FAILURE
  776.  
  777.     CREATE UNIQUE CLUSTERED INDEX ucMSagent_parameters ON msdb.dbo.MSagent_parameters
  778.         (parameter_name, profile_id)
  779.     if @@error<> 0 goto FAILURE
  780.  
  781.     /* 
  782.     ** Create default / non default profiles 
  783.     ** for all the agents 
  784.     */
  785.  
  786.     /* 
  787.     ** Snapshot agent 
  788.     */
  789.     set @profile_id = NULL
  790.     set @profile_name = formatmessage(20545) -- Default Snapshot Profile
  791.     set @profile_desc = NULL
  792.  
  793.     exec @retcode = dbo.sp_add_agent_profile
  794.             @profile_id = @profile_id OUT,
  795.             @profile_name = @profile_name,
  796.             @agent_type = 1,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  797.             @profile_type = 0,   -- 0-System, 1-Custom 
  798.             @description = @profile_desc,
  799.             @default = 1
  800.     if (@retcode = 1 or @@ERROR <> 0)
  801.         goto FAILURE
  802.  
  803.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  804.     if (@retcode = 1 or @@ERROR <> 0)
  805.         goto FAILURE
  806.  
  807.     /* 
  808.     ** Logreader agent 
  809.     */
  810.     set @profile_id = NULL
  811.     set @profile_name = formatmessage(20545) -- Default LogReader Profile
  812.     set @profile_desc = NULL
  813.  
  814.     exec @retcode = dbo.sp_add_agent_profile
  815.             @profile_id = @profile_id OUT,
  816.             @profile_name = @profile_name,
  817.             @agent_type = 2,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  818.             @profile_type = 0,   -- 0-System, 1-Custom 
  819.             @description = @profile_desc,
  820.             @default = 1
  821.     if (@retcode = 1 or @@ERROR <> 0)
  822.         goto FAILURE
  823.  
  824.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  825.     if (@retcode = 1 or @@ERROR <> 0)
  826.         goto FAILURE
  827.  
  828.     /* 
  829.     ** Logreader agent - Verbose History Profile
  830.     */
  831.     set @profile_id = NULL
  832.     set @profile_name = formatmessage(20546) -- LogReader Verbose History Profile
  833.     set @profile_desc = formatmessage(20547)
  834.  
  835.     exec @retcode = dbo.sp_add_agent_profile
  836.             @profile_id = @profile_id OUT,
  837.             @profile_name = @profile_name,
  838.             @agent_type = 2,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  839.             @profile_type = 0,   -- 0-System, 1-Custom 
  840.             @description = @profile_desc,
  841.             @default = 0
  842.     if (@retcode = 1 or @@ERROR <> 0)
  843.         goto FAILURE
  844.  
  845.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  846.     if (@retcode = 1 or @@ERROR <> 0)
  847.         goto FAILURE
  848.  
  849.     /* 
  850.     ** Distribution agent 
  851.     */
  852.     set @profile_id = NULL
  853.     set @profile_name = formatmessage(20545) -- Default Distribution Profile
  854.     set @profile_desc = NULL
  855.  
  856.     exec @retcode = dbo.sp_add_agent_profile
  857.             @profile_id = @profile_id OUT,
  858.             @profile_name = @profile_name,
  859.             @agent_type = 3,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  860.             @profile_type = 0,   -- 0-System, 1-Custom 
  861.             @description = @profile_desc,
  862.             @default = 1
  863.     if (@retcode = 1 or @@ERROR <> 0)
  864.         goto FAILURE
  865.  
  866.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  867.     if (@retcode = 1 or @@ERROR <> 0)
  868.         goto FAILURE
  869.  
  870.     /*
  871.     ** Distribution Agent Verbose History Profile
  872.     */
  873.     set @profile_id = NULL
  874.     set @profile_name = formatmessage(20546) -- Distribution Verbose History Profile
  875.     set @profile_desc = formatmessage(20547)
  876.  
  877.     exec @retcode = dbo.sp_add_agent_profile
  878.             @profile_id = @profile_id OUT,
  879.             @profile_name = @profile_name,
  880.             @agent_type = 3,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  881.             @profile_type = 0,   -- 0-System, 1-Custom 
  882.             @description = @profile_desc,
  883.             @default = 0
  884.     if (@retcode = 1 or @@ERROR <> 0)
  885.         goto FAILURE
  886.  
  887.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  888.     if (@retcode = 1 or @@ERROR <> 0)
  889.         goto FAILURE
  890.  
  891.     /* 
  892.     ** Merge agent : Default profile for well connected scenarios 
  893.     */
  894.     set @profile_id = NULL
  895.     set @profile_name = formatmessage(20545) -- Default Merge Profile
  896.     set @profile_desc = NULL
  897.  
  898.     exec @retcode = dbo.sp_add_agent_profile
  899.             @profile_id = @profile_id OUT,
  900.             @profile_name = @profile_name,
  901.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  902.             @profile_type = 0,   -- 0-System, 1-Custom 
  903.             @description = @profile_desc,
  904.             @default = 1
  905.  
  906.     if (@retcode = 1 or @@ERROR <> 0)
  907.         goto FAILURE
  908.  
  909.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  910.     if (@retcode = 1 or @@ERROR <> 0)
  911.         goto FAILURE
  912.  
  913.     /* 
  914.     ** Merge agent : Non default profile for disconnected scenarios ( unreliable link ) 
  915.     */
  916.     set @profile_id = NULL
  917.     set @profile_name = formatmessage(20548) -- Non-Default Merge Profile
  918.     set @profile_desc = formatmessage(20549)
  919.  
  920.     exec @retcode = dbo.sp_add_agent_profile
  921.             @profile_id = @profile_id OUT,
  922.             @profile_name = @profile_name,
  923.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  924.             @profile_type = 0,   -- 0-System, 1-Custom 
  925.             @description = @profile_desc,
  926.             @default = 0
  927.  
  928.     if (@retcode = 1 or @@ERROR <> 0)
  929.         goto FAILURE
  930.  
  931.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  932.     if (@retcode = 1 or @@ERROR <> 0)
  933.         goto FAILURE
  934.  
  935.     /* 
  936.     ** Merge agent : Non default profile for verbose histroy
  937.     */
  938.     set @profile_id = NULL
  939.     set @profile_name = formatmessage(20546) -- Verbose Merge Profile
  940.     set @profile_desc = formatmessage(20547)
  941.  
  942.     exec @retcode = dbo.sp_add_agent_profile
  943.             @profile_id = @profile_id OUT,
  944.             @profile_name = @profile_name,
  945.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  946.             @profile_type = 0,   -- 0-System, 1-Custom 
  947.             @description = @profile_desc,
  948.             @default = 0
  949.  
  950.     if (@retcode = 1 or @@ERROR <> 0)
  951.         goto FAILURE
  952.  
  953.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  954.     if (@retcode = 1 or @@ERROR <> 0)
  955.         goto FAILURE
  956.  
  957.     /* 
  958.     ** Merge agent : Synchronization Manager Profile
  959.     */
  960.     set @profile_id = NULL
  961.     set @profile_name = formatmessage(20550) -- SyncMgr Profile
  962.     set @profile_desc = formatmessage(20551)
  963.  
  964.     exec @retcode = dbo.sp_add_agent_profile
  965.             @profile_id = @profile_id OUT,
  966.             @profile_name = @profile_name,
  967.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  968.             @profile_type = 0,   -- 0-System, 1-Custom 
  969.             @description = @profile_desc,
  970.             @default = 0
  971.  
  972.     if (@retcode = 1 or @@ERROR <> 0)
  973.         goto FAILURE
  974.  
  975.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  976.     if (@retcode = 1 or @@ERROR <> 0)
  977.         goto FAILURE
  978.  
  979.     /* 
  980.     ** Distribution agent : Synchronization Manager Profile
  981.     */
  982.     set @profile_id = NULL
  983.     set @profile_name = formatmessage(20550) -- SyncMgr Profile
  984.     set @profile_desc = formatmessage(20551)
  985.  
  986.     exec @retcode = dbo.sp_add_agent_profile
  987.             @profile_id = @profile_id OUT,
  988.             @profile_name = @profile_name,
  989.             @agent_type = 3,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  990.             @profile_type = 0,   -- 0-System, 1-Custom 
  991.             @description = @profile_desc,
  992.             @default = 0
  993.  
  994.     if (@retcode = 1 or @@ERROR <> 0)
  995.         goto FAILURE
  996.  
  997.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  998.     if (@retcode = 1 or @@ERROR <> 0)
  999.         goto FAILURE
  1000.  
  1001.     /* 
  1002.     ** Qreader agent (default profile)
  1003.     */
  1004.     set @profile_id = NULL
  1005.     set @profile_name = formatmessage(20545) -- Default QueueReader Profile
  1006.     set @profile_desc = formatmessage(20589)
  1007.  
  1008.     exec @retcode = dbo.sp_add_agent_profile
  1009.             @profile_id = @profile_id OUT,
  1010.             @profile_name = @profile_name,
  1011.             @agent_type = 9,
  1012.             @profile_type = 0, 
  1013.             @description = @profile_desc,
  1014.             @default = 1
  1015.     if (@retcode = 1 or @@ERROR <> 0)
  1016.         goto FAILURE
  1017.  
  1018.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  1019.     if (@retcode = 1 or @@ERROR <> 0)
  1020.         goto FAILURE
  1021.  
  1022.     /* 
  1023.     ** Merge agent : Rowcount Validation profile  
  1024.     */
  1025.     set @profile_id = NULL
  1026.     set @profile_name = formatmessage(21308) -- Rowcount Validation Profile
  1027.     set @profile_desc = formatmessage(21309) -- Rowcount Validation Profile Description
  1028.  
  1029.     exec @retcode = dbo.sp_add_agent_profile
  1030.             @profile_id = @profile_id OUT,
  1031.             @profile_name = @profile_name,
  1032.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  1033.             @profile_type = 0,   -- 0-System, 1-Custom 
  1034.             @description = @profile_desc,
  1035.             @default = 0
  1036.  
  1037.     if (@retcode = 1 or @@ERROR <> 0)
  1038.         goto FAILURE
  1039.  
  1040.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  1041.     if (@retcode = 1 or @@ERROR <> 0)
  1042.         goto FAILURE
  1043.  
  1044.     /* 
  1045.     ** Merge agent : Rowcount & Checksum Validation profile  
  1046.     */
  1047.     set @profile_id = NULL
  1048.     set @profile_name = formatmessage(21310) -- Rowcount & Checksum Validation Profile
  1049.     set @profile_desc = formatmessage(21311) -- Rowcount & Checksum Validation Profile Description
  1050.  
  1051.     exec @retcode = dbo.sp_add_agent_profile
  1052.             @profile_id = @profile_id OUT,
  1053.             @profile_name = @profile_name,
  1054.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  1055.             @profile_type = 0,   -- 0-System, 1-Custom 
  1056.             @description = @profile_desc,
  1057.             @default = 0
  1058.  
  1059.     if (@retcode = 1 or @@ERROR <> 0)
  1060.         goto FAILURE
  1061.  
  1062.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  1063.     if (@retcode = 1 or @@ERROR <> 0)
  1064.         goto FAILURE
  1065.  
  1066.  
  1067.     /* 
  1068.     ** Distribution agent, skip error profile
  1069.     */
  1070.     set @profile_id = NULL
  1071.     set @profile_name = formatmessage(20599) -- Default Distribution Profile
  1072.     set @profile_desc = formatmessage(20600)
  1073.  
  1074.     exec @retcode = dbo.sp_add_agent_profile
  1075.             @profile_id = @profile_id OUT,
  1076.             @profile_name = @profile_name,
  1077.             @agent_type = 3,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  1078.             @profile_type = 0,   -- 0-System, 1-Custom 
  1079.             @description = @profile_desc,
  1080.             @default = 0
  1081.     if (@retcode = 1 or @@ERROR <> 0)
  1082.         goto FAILURE
  1083.  
  1084.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  1085.     if (@retcode = 1 or @@ERROR <> 0)
  1086.         goto FAILURE
  1087.  
  1088.     /* 
  1089.     ** Merge agent : High volume server-to-server profile
  1090.     */
  1091.     set @profile_id = NULL
  1092.     set @profile_name = formatmessage(20616) -- High volume server-to-server profile
  1093.     set @profile_desc = formatmessage(20617) -- High volume server-to-server profile Description
  1094.  
  1095.     exec @retcode = dbo.sp_add_agent_profile
  1096.             @profile_id = @profile_id OUT,
  1097.             @profile_name = @profile_name,
  1098.             @agent_type = 4,   -- 1-Snapshot, 2-Logreader, 3-Distribution, 4-Merge
  1099.             @profile_type = 0,   -- 0-System, 1-Custom 
  1100.             @description = @profile_desc,
  1101.             @default = 0
  1102.  
  1103.     if (@retcode = 1 or @@ERROR <> 0)
  1104.         goto FAILURE
  1105.  
  1106.     exec @retcode = dbo.sp_generate_agent_parameter @profile_id
  1107.     if (@retcode = 1 or @@ERROR <> 0)
  1108.         goto FAILURE
  1109.  
  1110.     create table msdb.dbo.MSdatatype_mappings 
  1111.     (
  1112.     dbms_name           sysname NOT NULL,
  1113.     sql_type            sysname NOT NULL,
  1114.     dest_type           sysname NOT NULL,
  1115.     dest_prec           int     NOT NULL,
  1116.     dest_create_params  int     NOT NULL,
  1117.     dest_nullable       bit     NOT NULL
  1118.     )
  1119.  
  1120.     -- MS Jet
  1121.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'binary' , 'binary', 255, 4, 1
  1122.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'varbinary' , 'varbinary', 255, 4, 1
  1123.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'binary' , 'image', 1073741824, 0, 1
  1124.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'varbinary' , 'image', 1073741824, 0, 1
  1125.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'sql_variant' , 'longtext', 1073741824, 0, 1
  1126.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'varchar' , 'varchar', 255, 4, 1
  1127.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'varchar' , 'longtext', 1073741824, 0, 1
  1128.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'nchar' , 'nchar', 255, 4, 1
  1129.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'nchar' , 'longtext', 1073741824, 0, 1
  1130.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'char' , 'char', 255, 4, 1
  1131.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'char' , 'longtext', 1073741824, 0, 1
  1132.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'nvarchar' , 'nchar varying', 255, 4, 1
  1133.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'nvarchar' , 'longtext', 1073741824, 0, 1
  1134.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'datetime' , 'datetime', 255, 0, 1
  1135.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'smalldatetime' , 'datetime', 255, 0, 1
  1136.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'decimal' , 'decimal', 255, 3, 1
  1137.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'numeric' , 'decimal', 255, 3, 1
  1138.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'float' , 'float', 255, 0, 1
  1139.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'real' , 'real', 255, 0, 1
  1140.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'bigint' , 'decimal', 255, 0, 1
  1141.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'int' , 'int', 255, 0, 1
  1142.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'smallint' , 'smallint', 255, 0, 1
  1143.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'tinyint' , 'byte', 255, 0, 1
  1144.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'money' , 'currency', 255, 0, 1
  1145.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'smallmoney' , 'currency', 255, 0, 1
  1146.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'bit' , 'bit', 255, 0, 1
  1147.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'sysname' , 'nchar varying', 255, 4, 1
  1148.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'timestamp' , 'binary', 255, 4, 1
  1149.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'uniqueidentifier' , 'guid', 255, 0, 1
  1150.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'text' , 'longtext', 1073741824, 0, 1
  1151.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'ntext' , 'longtext', 1073741824, 0, 1
  1152.     exec dbo.sp_add_datatype_mapping 'MS Jet', 'image' , 'image', 1073741824, 0, 1
  1153.  
  1154.     -- Oracle
  1155.     exec dbo.sp_add_datatype_mapping 'Oracle', 'binary' , 'raw', 255, 4, 1
  1156.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varbinary' , 'raw', 255, 4, 1
  1157.     exec dbo.sp_add_datatype_mapping 'Oracle', 'binary' , 'long raw', 2147483647, 0, 1
  1158.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varbinary' , 'long raw', 2147483647, 0, 1
  1159.     exec dbo.sp_add_datatype_mapping 'Oracle', 'sql_variant' , 'long', 2147483647, 0, 1
  1160.     --exec dbo.sp_add_datatype_mapping 'Oracle', 'varchar' , 'char', 255, 4, 1
  1161.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varchar' , 'varchar2', 2000, 4, 1
  1162.     exec dbo.sp_add_datatype_mapping 'Oracle', 'varchar' , 'long', 2147483647, 0, 1
  1163.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nchar' , 'char', 255, 4, 1
  1164.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nchar' , 'varchar2', 2000, 4, 1
  1165.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nchar' , 'long', 2147483647, 0, 1
  1166.     exec dbo.sp_add_datatype_mapping 'Oracle', 'char' , 'char', 255, 4, 1
  1167.     exec dbo.sp_add_datatype_mapping 'Oracle', 'char' , 'varchar2', 2000, 4, 1
  1168.     exec dbo.sp_add_datatype_mapping 'Oracle', 'char' , 'long', 2147483647, 0, 1
  1169.     --exec dbo.sp_add_datatype_mapping 'Oracle', 'nvarchar' , 'char', 255, 4, 1
  1170.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nvarchar' , 'varchar2', 2000, 4, 1
  1171.     exec dbo.sp_add_datatype_mapping 'Oracle', 'nvarchar' , 'long', 2147483647, 0, 1
  1172.     exec dbo.sp_add_datatype_mapping 'Oracle', 'datetime' , 'date', 255, 0, 1
  1173.     exec dbo.sp_add_datatype_mapping 'Oracle', 'smalldatetime' , 'date', 255, 0, 1
  1174.     exec dbo.sp_add_datatype_mapping 'Oracle', 'decimal' , 'number', 255, 3, 1
  1175.     exec dbo.sp_add_datatype_mapping 'Oracle', 'numeric' , 'number', 255, 3, 1
  1176.     exec dbo.sp_add_datatype_mapping 'Oracle', 'float' , 'float', 255, 0, 1
  1177.     exec dbo.sp_add_datatype_mapping 'Oracle', 'real' , 'float', 255, 0, 1
  1178.     exec dbo.sp_add_datatype_mapping 'Oracle', 'bigint' , 'number', 255, 3, 1
  1179.     exec dbo.sp_add_datatype_mapping 'Oracle', 'int' , 'number', 255, 3, 1
  1180.     exec dbo.sp_add_datatype_mapping 'Oracle', 'smallint' , 'number', 255, 3, 1
  1181.     exec dbo.sp_add_datatype_mapping 'Oracle', 'tinyint' , 'number', 255, 3, 1
  1182.     exec dbo.sp_add_datatype_mapping 'Oracle', 'money' , 'number', 255, 3, 1
  1183.     exec dbo.sp_add_datatype_mapping 'Oracle', 'smallmoney' , 'number', 255, 3, 1
  1184.     exec dbo.sp_add_datatype_mapping 'Oracle', 'bit' , 'number', 255, 3, 1
  1185.     exec dbo.sp_add_datatype_mapping 'Oracle', 'sysname' , 'char', 255, 4, 1
  1186.     exec dbo.sp_add_datatype_mapping 'Oracle', 'timestamp' , 'raw', 255, 4, 1
  1187.     exec dbo.sp_add_datatype_mapping 'Oracle', 'uniqueidentifier' , 'char', 255, 4, 1
  1188.     exec dbo.sp_add_datatype_mapping 'Oracle', 'text' , 'long', 2147483647, 0, 1
  1189.     exec dbo.sp_add_datatype_mapping 'Oracle', 'ntext' , 'long', 2147483647, 0, 1
  1190.     exec dbo.sp_add_datatype_mapping 'Oracle', 'image' , 'long raw', 2147483647, 0, 1
  1191.  
  1192.     -- MS SSCE
  1193.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'binary' , 'binary', 255, 4, 1
  1194.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'varbinary' , 'varbinary', 255, 4, 1
  1195.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'binary' , 'image', 1073741824, 0, 1
  1196.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'varbinary' , 'image', 1073741824, 0, 1
  1197.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'sql_variant' , 'ntext', 1073741824, 0, 1
  1198.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'varchar' , 'national char varying', 255, 4, 1
  1199.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'varchar' , 'ntext', 1073741824, 0, 1
  1200.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'nchar' , 'nchar', 255, 4, 1
  1201.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'nchar' , 'ntext', 1073741824, 0, 1
  1202.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'char' , 'nchar', 255, 4, 1
  1203.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'char' , 'ntext', 1073741824, 0, 1
  1204.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'nvarchar' , 'national char varying', 255, 4, 1
  1205.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'nvarchar' , 'ntext', 1073741824, 0, 1
  1206.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'datetime' , 'datetime', 255, 0, 1
  1207.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'smalldatetime' , 'datetime', 255, 0, 1
  1208.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'decimal' , 'numeric', 255, 3, 1
  1209.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'numeric' , 'numeric', 255, 3, 1
  1210.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'float' , 'float', 255, 0, 1
  1211.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'real' , 'real', 255, 0, 1
  1212.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'bigint' , 'bigint', 255, 0, 1
  1213.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'int' , 'int', 255, 0, 1
  1214.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'smallint' , 'smallint', 255, 0, 1
  1215.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'tinyint' , 'tinyint', 255, 0, 1
  1216.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'money' , 'money', 255, 0, 1
  1217.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'smallmoney' , 'money', 255, 0, 1
  1218.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'bit' , 'bit', 255, 0, 1
  1219.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'sysname' , 'national char varying', 255, 4, 1
  1220.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'timestamp' , 'binary', 255, 4, 1
  1221.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'uniqueidentifier' , 'uniqueidentifier', 255, 0, 1
  1222.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'text' , 'ntext', 1073741824, 0, 1
  1223.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'ntext' , 'ntext', 1073741824, 0, 1
  1224.     exec dbo.sp_add_datatype_mapping 'MS SSCE', 'image' , 'image', 1073741824, 0, 1
  1225.  
  1226.     --DB2/400
  1227.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'bit', 'SMALLINT', 1,  0, 1
  1228.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'tinyint', 'SMALLINT', 3, 0, 1
  1229.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'smallint', 'SMALLINT', 5, 0, 1
  1230.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'int', 'INT', 10, 0, 1
  1231.  
  1232.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'char', 'CHAR', 8000, 4, 1
  1233.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'varchar', 'VARCHAR', 8000, 4, 1
  1234.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'smalldatetime', 'TIMESTAMP', 26, 0, 1
  1235.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'datetime', 'TIMESTAMP', 26, 0, 1
  1236.     
  1237.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'real', 'REAL', 24, 0, 1
  1238.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'decimal', 'DECIMAL', 31, 3, 1
  1239.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'double precision', 'DOUBLE', 53, 0, 1
  1240.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'float', 'FLOAT', 53, 0, 1
  1241.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'numeric', 'NUMERIC', 31, 3, 1
  1242.  
  1243.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'smallmoney', 'DECIMAL', 10, 3, 1
  1244.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'money', 'DECIMAL', 19, 3, 1
  1245.  
  1246.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'varbinary', 'VARCHAR () FOR BIT DATA', 8000, 4, 1
  1247.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'binary', 'CHAR () FOR BIT DATA', 8000, 4, 1
  1248.  
  1249.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'timestamp', 'CHAR () FOR BIT DATA', 8, 4, 1
  1250.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'uniqueidentifier', 'CHAR', 38, 4, 1
  1251.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'image', 'VARCHAR () FOR BIT DATA', 32739, 4, 1
  1252.     exec dbo.sp_add_datatype_mapping 'DB2/400', 'text', 'VARCHAR', 32739, 4, 1
  1253.  
  1254.     --DB2/MVS
  1255.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'bit', 'SMALLINT', 1,  0, 1
  1256.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'tinyint', 'SMALLINT', 3, 0, 1
  1257.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'smallint', 'SMALLINT', 5, 0, 1
  1258.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'int', 'INT', 10, 0, 1
  1259.  
  1260.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'char', 'CHAR', 254, 4, 1
  1261.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'varchar', 'VARCHAR', 4045, 4, 1
  1262.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'char', 'VARCHAR', 4045, 4, 1
  1263.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'smalldatetime', 'TIMESTAMP', 26, 0, 1
  1264.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'datetime', 'TIMESTAMP', 26, 0, 1
  1265.  
  1266.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'real', 'REAL', 24, 0, 1
  1267.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'decimal', 'DECIMAL', 31, 3, 1
  1268.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'double precision', 'DOUBLE', 53, 0, 1
  1269.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'float', 'FLOAT', 53, 0, 1
  1270.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'numeric', 'NUMERIC', 31, 3, 1
  1271.  
  1272.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'smallmoney', 'DECIMAL', 10, 3, 1
  1273.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'money', 'DECIMAL', 19, 3, 1
  1274.  
  1275.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'varbinary', 'VARCHAR () FOR BIT DATA', 4045, 4, 1
  1276.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'binary', 'CHAR () FOR BIT DATA', 254, 4, 1
  1277.  
  1278.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'timestamp', 'CHAR () FOR BIT DATA', 8, 4, 1
  1279.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'uniqueidentifier', 'CHAR', 38, 4, 1
  1280.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'image', 'VARCHAR () FOR BIT DATA', 4045, 4, 1
  1281.     exec dbo.sp_add_datatype_mapping 'DB2/MVS', 'text', 'VARCHAR', 4045, 4, 1
  1282.  
  1283.     --DB2/NT
  1284.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'bit', 'SMALLINT', 1,  0, 1
  1285.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'tinyint', 'SMALLINT', 3, 0, 1
  1286.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'smallint', 'SMALLINT', 5, 0, 1
  1287.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'int', 'INT', 10, 0, 1
  1288.  
  1289.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'char', 'CHAR', 254, 4, 1
  1290.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'varchar', 'VARCHAR', 4000, 4, 1
  1291.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'char', 'VARCHAR', 4000, 4, 1
  1292.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'smalldatetime', 'TIMESTAMP', 26, 0, 1
  1293.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'datetime', 'TIMESTAMP', 26, 0, 1
  1294.  
  1295.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'real', 'REAL', 24, 0, 1
  1296.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'decimal', 'DECIMAL', 31, 3, 1
  1297.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'double precision', 'DOUBLE', 53, 0, 1
  1298.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'float', 'FLOAT', 53, 0, 1
  1299.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'numeric', 'NUMERIC', 31, 3, 1
  1300.  
  1301.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'smallmoney', 'DECIMAL', 10, 3, 1
  1302.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'money', 'DECIMAL', 19, 3, 1
  1303.  
  1304.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'varbinary', 'VARCHAR () FOR BIT DATA', 4000, 4, 1
  1305.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'binary', 'CHAR () FOR BIT DATA', 254, 4, 1
  1306.  
  1307.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'timestamp', 'CHAR () FOR BIT DATA', 8, 4, 1
  1308.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'uniqueidentifier', 'CHAR', 38, 4, 1
  1309.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'image', 'VARCHAR () FOR BIT DATA', 4000, 4, 1
  1310.     exec dbo.sp_add_datatype_mapping 'DB2/NT', 'text', 'VARCHAR', 4000, 4, 1
  1311.  
  1312.     --DB2/6000
  1313.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'bit', 'SMALLINT', 1,  0, 1
  1314.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'tinyint', 'SMALLINT', 3, 0, 1
  1315.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'smallint', 'SMALLINT', 5, 0, 1
  1316.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'int', 'INT', 10, 0, 1
  1317.  
  1318.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'char', 'CHAR', 254, 4, 1
  1319.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'varchar', 'VARCHAR', 4000, 4, 1
  1320.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'char', 'VARCHAR', 4000, 4, 1
  1321.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'smalldatetime', 'TIMESTAMP', 26, 0, 1
  1322.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'datetime', 'TIMESTAMP', 26, 0, 1
  1323.  
  1324.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'real', 'REAL', 24, 0, 1
  1325.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'decimal', 'DECIMAL', 31, 3, 1
  1326.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'double precision', 'DOUBLE', 53, 0, 1
  1327.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'float', 'FLOAT', 53, 0, 1
  1328.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'numeric', 'NUMERIC', 31, 3, 1
  1329.  
  1330.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'smallmoney', 'DECIMAL', 10, 3, 1
  1331.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'money', 'DECIMAL', 19, 3, 1
  1332.  
  1333.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'varbinary', 'VARCHAR () FOR BIT DATA', 4000, 4, 1
  1334.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'binary', 'CHAR () FOR BIT DATA', 254, 4, 1
  1335.  
  1336.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'timestamp', 'CHAR () FOR BIT DATA', 8, 4, 1
  1337.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'uniqueidentifier', 'CHAR', 38, 4, 1
  1338.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'image', 'VARCHAR () FOR BIT DATA', 4000, 4, 1
  1339.     exec dbo.sp_add_datatype_mapping 'DB2/6000', 'text', 'VARCHAR', 4000, 4, 1
  1340.  
  1341.     return 0
  1342. FAILURE:
  1343.     return 1
  1344. go
  1345.  
  1346. dump tran master with no_log
  1347. GO
  1348.  
  1349. /*
  1350. ** Create replication stored procedures and functions.
  1351. ** Part 2:  create all other stored procedures and functions.
  1352. */
  1353. --
  1354. -- Name: fn_getpersistedservernamecasevariation
  1355. --
  1356. -- Description: A simple function for getting the case variation of a
  1357. --              server name as persisted in master..sysservers. For
  1358. --              example, if sErVeRnAmE is what is being persisted 
  1359. --              as a srvname in sysservers
  1360. --
  1361. --              select fn_getpersistedservernamecasevariation('SERVERNAME')
  1362. --              select fn_getpersistedservernamecasevariation('servername')
  1363. --            
  1364. --              will all return the string 'sErVeRnAmE'                
  1365. --
  1366. -- Parameter: @servername sysname (mandatory)
  1367. --
  1368. -- Notes: This function will return null if the specified server name
  1369. --        does not match any srvname's in master..sysservers in a
  1370. --        case insensitive manner.
  1371. --
  1372. -- Security: Admin access only
  1373. -- 
  1374. raiserror('Creating function fn_getpersistedservernamecasevariation', 0,1)
  1375. go
  1376. create function system_function_schema.fn_getpersistedservernamecasevariation (
  1377.     @servername sysname
  1378.     ) returns sysname
  1379. as
  1380. begin
  1381.  
  1382.     declare @real_servername sysname
  1383.     select @real_servername = null
  1384.     select @real_servername = srvname 
  1385.       from master.dbo.sysservers
  1386.      where upper(@servername) = upper(srvname) collate database_default 
  1387.  
  1388.     return @real_servername
  1389. end
  1390. go
  1391.  
  1392. raiserror('Creating procedure sp_MSIfExistsRemoteLogin', 0,1)
  1393. GO
  1394.  
  1395. CREATE proc sp_MSIfExistsRemoteLogin
  1396.     @remotesrvname sysname,
  1397.     @locallgname   sysname,
  1398.     @remotelgname  sysname
  1399. AS
  1400. begin
  1401.  
  1402.  
  1403.     if (@locallgname IS NULL)
  1404.     begin
  1405.      IF EXISTS (SELECT * FROM master.dbo.sysremotelogins srl, master.dbo.sysservers ss
  1406.                  WHERE UPPER(ss.srvname) = UPPER(@remotesrvname) collate database_default AND 
  1407.                        srl.remoteserverid = ss.srvid AND 
  1408.                        (srl.remoteusername = @remotelgname OR 
  1409.                        (srl.remoteusername IS NULL AND srl.sid = 0x2)))
  1410.         return (1)
  1411.     else
  1412.         return (0)
  1413.     end
  1414.  
  1415.     if exists (SELECT * FROM master.dbo.sysremotelogins srl, master.dbo.sysservers ss 
  1416.                                 WHERE UPPER(ss.srvname) = UPPER(@remotesrvname) collate database_default AND 
  1417.                                       srl.remoteserverid = ss.srvid AND 
  1418.                                       srl.remoteusername = @remotelgname AND 
  1419.                                       srl.sid = suser_sid(@locallgname))
  1420.         return (1)
  1421.     else
  1422.         return (0)
  1423. end
  1424. go
  1425.  
  1426. raiserror('Creating procedure sp_helppublicationsync', 0,1)
  1427. GO
  1428.  
  1429. CREATE PROCEDURE sp_helppublicationsync (
  1430.         @publication sysname    /* The publication name */
  1431.         ) AS
  1432.     SET NOCOUNT ON
  1433.     RAISERROR (21023, 16, -1,'sp_helppublicationsync')
  1434.     RETURN(1)
  1435. GO
  1436.  
  1437. raiserror('Creating procedure sp_MSreplrole', 0,1)
  1438. GO
  1439. create procedure sp_MSreplrole
  1440. @name sysname,
  1441. @operation nvarchar(4)
  1442. as
  1443. declare @retcode int
  1444. -- Add/Drop when proper.
  1445. if @operation = 'add'
  1446. begin
  1447.     if user_id(@name) is null
  1448.     begin
  1449.         exec @retcode = dbo.sp_addrole @name
  1450.         IF @@ERROR <> 0 or @retcode <> 0
  1451.             RETURN (1)
  1452.     end
  1453. end
  1454. else if @operation = 'drop' 
  1455. begin
  1456.     if user_id(@name) is not null
  1457.     begin
  1458.         exec @retcode = dbo.sp_droprole @name
  1459.         IF @@ERROR <> 0 or @retcode <> 0
  1460.             RETURN (1)
  1461.     end
  1462. end
  1463. go
  1464.  
  1465. dump tran master with no_log
  1466. GO
  1467.  
  1468. raiserror('Creating procedure sp_addpublication_snapshot', 0,1)
  1469. GO
  1470.  
  1471. CREATE PROCEDURE sp_addpublication_snapshot (
  1472.     @publication sysname,    
  1473.     @frequency_type  int = 4 ,              /* 4== Daily */
  1474.     @frequency_interval int = 1,            /* Every day */
  1475.     @frequency_subday int = 4,              /* Sub interval = Minute */
  1476.     @frequency_subday_interval int = 5,     /* Every five minutes */
  1477.     @frequency_relative_interval int = 1, 
  1478.     @frequency_recurrence_factor int = 0, 
  1479.     @active_start_date int = 0, 
  1480.     @active_end_date int = 99991231 , 
  1481.     @active_start_time_of_day int = 0, 
  1482.     @active_end_time_of_day int = 235959,
  1483.     @snapshot_job_name nvarchar(100) = NULL         
  1484. ) AS
  1485.  
  1486.     SET NOCOUNT ON
  1487.  
  1488.     /*
  1489.     ** Declarations.
  1490.     */
  1491.     DECLARE @retcode                int
  1492.     DECLARE @newid                  int
  1493.     DECLARE @mergepublish_bit       smallint
  1494.     DECLARE @transpublish_bit       int
  1495.     DECLARE @transpub_found         bit
  1496.     DECLARE @mergepub_found         bit
  1497.     DECLARE @newagentid             int
  1498.  
  1499.     /*
  1500.     ** Initializations
  1501.     */
  1502.     select @mergepublish_bit    = 4
  1503.     select @transpublish_bit    = 1
  1504.     select @transpub_found      = 0
  1505.     select @mergepub_found      = 0
  1506.     
  1507.     /*
  1508.     ** Parameter Check
  1509.     */
  1510.     exec @retcode = dbo.sp_MSreplcheck_name @publication
  1511.     if @@ERROR <> 0 or @retcode <> 0
  1512.         return(1)
  1513.  
  1514.     /*
  1515.     ** Security Check
  1516.     */
  1517.     exec @retcode = dbo.sp_MSreplcheck_publish
  1518.     if @@ERROR <> 0 or @retcode <> 0
  1519.         return(1)
  1520.  
  1521.     /*
  1522.     **  Check if the publication is valid.
  1523.     **  1. Check transaction-level publications
  1524.     **  2. Check merge publications
  1525.     */
  1526.     if (select category & @transpublish_bit from master..sysdatabases where name = DB_NAME() collate database_default) <> 0
  1527.     begin
  1528.         EXEC @retcode = dbo.sp_MSaddpub_snapshot     @publication ,    
  1529.             @frequency_type,  
  1530.             @frequency_interval, 
  1531.             @frequency_subday, 
  1532.             @frequency_subday_interval,
  1533.             @frequency_relative_interval, 
  1534.             @frequency_recurrence_factor, 
  1535.             @active_start_date, 
  1536.             @active_end_date, 
  1537.             @active_start_time_of_day, 
  1538.             @active_end_time_of_day,         
  1539.             @newagentid OUTPUT,
  1540.             @snapshot_job_name 
  1541.  
  1542.         IF @retcode <> 0 and @@ERROR <> 0 
  1543.             BEGIN
  1544.                 RETURN (1)
  1545.             END
  1546.  
  1547.         if @newagentid <> 0
  1548.             begin
  1549.                 select @transpub_found = 1
  1550.                 goto DONE
  1551.             end             
  1552.  
  1553.     end
  1554.     
  1555.     if (select category & @mergepublish_bit from master..sysdatabases where name = DB_NAME() collate database_default) <> 0
  1556.     begin
  1557.         EXEC @retcode = dbo.sp_MSaddmergepub_snapshot     @publication ,    
  1558.             @frequency_type,  
  1559.             @frequency_interval, 
  1560.             @frequency_subday, 
  1561.             @frequency_subday_interval,
  1562.             @frequency_relative_interval, 
  1563.             @frequency_recurrence_factor, 
  1564.             @active_start_date, 
  1565.             @active_end_date, 
  1566.             @active_start_time_of_day, 
  1567.             @active_end_time_of_day,         
  1568.             @newagentid OUTPUT,
  1569.             @snapshot_job_name
  1570.  
  1571.         IF @retcode <> 0 and @@ERROR <> 0 
  1572.             BEGIN
  1573.                 RETURN (1)
  1574.             END
  1575.  
  1576.         if @newagentid <> 0
  1577.             begin
  1578.                 select @mergepub_found = 1
  1579.                 goto DONE
  1580.             end             
  1581.     end
  1582.  
  1583. DONE:
  1584.     if @transpub_found = 0 and @mergepub_found = 0
  1585.     begin
  1586.         RAISERROR (15001, 11, -1, @publication)
  1587.         RETURN (1)
  1588.     end
  1589.     return (0)        
  1590. GO
  1591.  
  1592. raiserror('Creating procedure sp_MShelpobjectpublications', 0,1)
  1593. GO
  1594.  
  1595. create procedure sp_MShelpobjectpublications (@object_name sysname)
  1596. AS
  1597.     /*
  1598.     ** Declarations.
  1599.     */
  1600.     DECLARE @retcode                int
  1601.     DECLARE @mergepublish_bit       smallint
  1602.     DECLARE @transpublish_bit       int
  1603.  
  1604.     declare @object_id int
  1605.  
  1606.     /*
  1607.     ** Initializations
  1608.     */
  1609.     select @mergepublish_bit    = 4
  1610.     select @transpublish_bit    = 1
  1611.     select @object_id           = OBJECT_ID(@object_name)
  1612.  
  1613.     create table #helpobjpubs (
  1614.         publication     sysname         collate database_default not null, 
  1615.         reptype         int             NOT NULL, 
  1616.         article         sysname         collate database_default not null, 
  1617.         article_type    int             NULL, 
  1618.         column_tracking int             NULL, 
  1619.         article_resolver nvarchar(255)  collate database_default null,
  1620.         identity_support int             NULL, 
  1621.         resolver_clsid nvarchar(50)        NULL,
  1622.         resolver_info  nvarchar(255)    NULL,
  1623.         verify_resolver_signature int     NULL,
  1624.         allow_interactive_resolver bit     NULL,
  1625.         fast_multicol_updateproc bit    NULL,
  1626.         check_permissions int            NULL)
  1627.     /*
  1628.     **  1. Return the transactional publications that the table is involved in
  1629.     */
  1630.     if (select category & @transpublish_bit from master..sysdatabases where name = DB_NAME() collate database_default) <> 0
  1631.     begin
  1632.         if exists (select * from sysextendedarticlesview a, 
  1633.             syspublications p where a.pubid = p.pubid and a.objid = @object_id)
  1634.             begin
  1635.                 insert into #helpobjpubs(publication, reptype, article, article_type) 
  1636.                     select p.name, 1, a.name, a.type  from sysextendedarticlesview a, 
  1637.                         syspublications p where a.pubid = p.pubid and a.objid = @object_id
  1638.                 IF @@ERROR <> 0 
  1639.                     BEGIN
  1640.                         select @retcode = 1
  1641.                         goto DONE
  1642.                     END
  1643.             end
  1644.     end
  1645.     /*
  1646.     **  2. Return the merge publications that the table is involved in
  1647.     */
  1648.     if exists (select * from sysobjects where name='sysmergepublications')
  1649.     begin
  1650.         if exists (select * from sysmergeextendedarticlesview a, 
  1651.             sysmergepublications p where a.pubid = p.pubid and a.objid = @object_id)
  1652.             begin
  1653.                 insert into #helpobjpubs (publication, reptype, article, article_type, column_tracking, article_resolver,
  1654.                 identity_support, resolver_clsid, resolver_info, verify_resolver_signature, allow_interactive_resolver,
  1655.                             fast_multicol_updateproc, check_permissions)
  1656.                     select p.name, 2, a.name, a.type, a.column_tracking, a.article_resolver, a.identity_support,
  1657.                             a.resolver_clsid, a.resolver_info, a.verify_resolver_signature, a.allow_interactive_resolver,
  1658.                             a.fast_multicol_updateproc, a.check_permissions
  1659.                         from sysmergeextendedarticlesview a, sysmergepublications p 
  1660.                         where a.pubid = p.pubid and a.objid = @object_id
  1661.                 IF @@ERROR <> 0 
  1662.                     BEGIN
  1663.                         select @retcode = 1
  1664.                         goto DONE
  1665.                     END
  1666.             end
  1667.     end
  1668.     select @retcode = 0
  1669.  
  1670. DONE:
  1671.     select * from #helpobjpubs
  1672.     drop table #helpobjpubs
  1673.     return (@retcode)
  1674. go
  1675.  
  1676. raiserror('Creating procedure sp_helpreplicationdb', 0,1)
  1677. GO
  1678.  
  1679. CREATE PROCEDURE sp_helpreplicationdb
  1680.         @dbname sysname = '%', @type sysname = 'pub'
  1681.     AS
  1682.  
  1683.     SET NOCOUNT ON
  1684.  
  1685.     /*
  1686.     ** Declarations.
  1687.     */
  1688.  
  1689.     DECLARE @retcode int, @typebit int
  1690.  
  1691.     if (lower(@type collate SQL_Latin1_General_CP1_CS_AS) like 'pub%')
  1692.        select @typebit = 1
  1693.     else if (lower(@type collate SQL_Latin1_General_CP1_CS_AS) like 'sub%')
  1694.        select @typebit = 2
  1695.     else
  1696.     begin
  1697.        raiserror(14091,-1,-1)
  1698.        return 1
  1699.     end
  1700.  
  1701.     /*
  1702.     ** Parameter Check:  @dbname.
  1703.     ** Check to make sure that the database name conforms to the rules
  1704.     ** for identifiers.
  1705.     */
  1706.  
  1707.     IF @dbname <> '%'
  1708.        BEGIN
  1709.           EXECUTE @retcode = dbo.sp_validname @dbname
  1710.  
  1711.           IF @@ERROR <> 0 OR @retcode <> 0
  1712.           RETURN (1)
  1713.        END
  1714.  
  1715.     /*
  1716.     ** Show databases with this option enabled.
  1717.     */
  1718.  
  1719.     SELECT name
  1720.       FROM master..sysdatabases
  1721.      WHERE ((@dbname = N'%') or (name = @dbname collate database_default)) 
  1722.        AND (category & @typebit) <> 0
  1723. go
  1724.  
  1725.  
  1726. raiserror('Creating procedure sp_helpdistributor', 0,1)
  1727. go
  1728.  
  1729. CREATE PROCEDURE sp_helpdistributor (
  1730.     @distributor sysname  = '%' OUTPUT, /* The distribution server name */
  1731.     @distribdb   sysname  = '%' OUTPUT, /* The distribution database */
  1732.     @directory   nvarchar(255) = '%' OUTPUT, /* The working directory */
  1733.     @account     nvarchar(255) = '%' OUTPUT, /* The Windows NT user account */
  1734.     @min_distretention int      = -1 OUTPUT, /* The min distribution retention */
  1735.     @max_distretention int      = -1 OUTPUT, /* The min distribution retention */
  1736.     @history_retention   int  = -1 OUTPUT, /* The history retention period */
  1737.     @history_cleanupagent nvarchar(100) = '%' OUTPUT, /* The history cleanup agent */
  1738.     @distrib_cleanupagent nvarchar(100) = '%' OUTPUT, /* The distribution cleanup agent */
  1739.     @publisher sysname = NULL,  /* Name of publisher */
  1740.     @local nvarchar(5) = NULL,        /* Get local server values */
  1741.     @rpcsrvname sysname = '%' OUTPUT
  1742. ) AS
  1743.     SET NOCOUNT ON
  1744.  
  1745.     /*
  1746.     ** Declarations.
  1747.     */
  1748.     DECLARE @loc_distributor         sysname
  1749.     DECLARE @loc_distribdb             sysname
  1750.     DECLARE @loc_directory             nvarchar(255)
  1751.     DECLARE @loc_account             nvarchar(255)
  1752.     DECLARE @loc_mindistretention     int
  1753.     DECLARE @loc_maxdistretention     int
  1754.     DECLARE @loc_historyretention   int  
  1755.     DECLARE @loc_historycleanupagent nvarchar(100)
  1756.     DECLARE @loc_distribcleanupagent nvarchar(100)
  1757.     DECLARE @loc_security_mode  int  
  1758.     DECLARE @loc_login sysname
  1759.     DECLARE @loc_password sysname
  1760.     declare @loc_rpcsrvname sysname
  1761.     DECLARE @proc nvarchar(255)
  1762.     DECLARE @reg_key nvarchar (255)
  1763.     DECLARE @retcode int
  1764.     declare @rpcsrvlogin sysname
  1765.     declare @srvid smallint
  1766.     declare @dist_rpcname sysname
  1767.     declare @platform_nt binary
  1768.  
  1769.     select @platform_nt = 0x1
  1770.  
  1771.     IF @publisher IS NULL
  1772.     BEGIN
  1773.         /* 
  1774.         ** 6.x compatibility 
  1775.         ** If local is set, we know the call is from a publisher. 
  1776.         ** set it to be @@REMSERVER 
  1777.         ** Otherwise, set it to be local server name
  1778.         ** Note: @@REMSERVER is NULL for local sp calls 
  1779.         */
  1780.         IF LOWER(@local) = 'local' AND @@REMSERVER IS NOT NULL
  1781.             SELECT @publisher = @@REMSERVER
  1782.         ELSE
  1783.             SELECT  @publisher = @@SERVERNAME
  1784.     END
  1785.  
  1786.     /*
  1787.     ** Get the distribution server
  1788.     */
  1789.     SELECT @dist_rpcname = srvname, @loc_distributor = datasource, @srvid = srvid, @loc_rpcsrvname = srvname
  1790.         FROM master..sysservers
  1791.         WHERE srvstatus & 8 <> 0
  1792.     
  1793.     if @loc_distributor is null
  1794.         GOTO DONE
  1795.  
  1796.     select @rpcsrvlogin = name from master.dbo.sysxlogins where
  1797.         srvid = @srvid and sid is NULL
  1798.  
  1799.     /*
  1800.     ** If remote distribuiton, execute dbo.sp_helpdistributor on distribution
  1801.     ** server.
  1802.     */
  1803.     IF UPPER(@loc_distributor) <> UPPER(@@SERVERNAME)
  1804.     BEGIN
  1805.         SELECT @proc = @dist_rpcname + '.master.dbo.sp_helpdistributor'
  1806.         EXECUTE @retcode = @proc
  1807.             @loc_distributor OUTPUT,
  1808.             @loc_distribdb OUTPUT,
  1809.             @loc_directory OUTPUT,
  1810.             @loc_account OUTPUT,
  1811.             @loc_mindistretention OUTPUT,
  1812.             @loc_maxdistretention OUTPUT,
  1813.             @loc_historyretention OUTPUT,
  1814.             @loc_historycleanupagent OUTPUT,
  1815.             @loc_distribcleanupagent OUTPUT,
  1816.             @@SERVERNAME,
  1817.             @local = 'local'
  1818.         IF @retcode <> 0 or @@ERROR <> 0
  1819.            RETURN (1)
  1820.  
  1821.             GOTO DONE
  1822.     END
  1823.  
  1824.     SELECT  @loc_distribdb = distribution_db,
  1825.             @loc_directory = working_directory
  1826.         FROM msdb.dbo.MSdistpublishers WHERE UPPER(name) = UPPER(@publisher) collate database_default
  1827.  
  1828.     IF @@ERROR <> 0
  1829.         RETURN 1 ;
  1830.  
  1831.     SELECT  @loc_mindistretention = min_distretention,      
  1832.             @loc_maxdistretention = max_distretention,
  1833.             @loc_historyretention = history_retention
  1834.         FROM msdb.dbo.MSdistributiondbs WHERE name = @loc_distribdb collate database_default 
  1835.  
  1836.     /*
  1837.     ** Fetch the distribution account name.
  1838.     */
  1839.     IF ((@distributor = '%' AND @distribdb = '%' AND @directory = '%'
  1840.     AND @account = '%' AND @min_distretention = -1 AND @max_distretention = -1 
  1841.     AND @history_retention = -1 AND @history_cleanupagent = '%' 
  1842.     AND @distrib_cleanupagent = '%' ) 
  1843.     OR @account IS NULL) and ( platform() & @platform_nt = @platform_nt ) 
  1844.        BEGIN
  1845.  
  1846.         declare @instance sysname
  1847.         declare @regkey nvarchar(260)
  1848.         select @instance = convert(sysname, SERVERPROPERTY('InstanceName'))
  1849.         select @regkey = 'SYSTEM\CurrentControlSet\Services\'
  1850.         -- default installation
  1851.         if @instance is null
  1852.             SELECT @regkey = @regkey + 'SQLServerAgent' 
  1853.         else
  1854.             SELECT @regkey = @regkey + 'SQLAgent$' + @instance
  1855.  
  1856.         SELECT @proc = 'master..xp_regread'
  1857.         EXECUTE @retcode = @proc 'HKEY_LOCAL_MACHINE',
  1858.               @regkey,
  1859.               'ObjectName',
  1860.             @param = @loc_account OUTPUT
  1861.     
  1862.         IF @@ERROR <> 0 OR @retcode <> 0
  1863.             SELECT @loc_account = NULL
  1864.        END
  1865.  
  1866.     /*
  1867.     ** Fetch the history cleanup agentname.
  1868.     */
  1869.     IF @loc_distribdb IS NOT NULL
  1870.         SELECT @loc_historycleanupagent = formatmessage (20567, @loc_distribdb)
  1871.  
  1872.     /*
  1873.     ** Fetch the distribution cleanup agent name.
  1874.     */
  1875.     IF @loc_distribdb IS NOT NULL
  1876.         SELECT @loc_distribcleanupagent = formatmessage (20568, @loc_distribdb)
  1877.  
  1878.  
  1879. DONE:
  1880.     /*
  1881.     ** Return result set if no output parameters
  1882.     */
  1883.  
  1884.     IF @distributor = '%' AND @distribdb = '%' AND @directory = '%'
  1885.     AND @account = '%' AND @min_distretention = -1 AND @max_distretention = -1 
  1886.     AND @history_retention = -1 AND @history_cleanupagent = '%' 
  1887.     AND @distrib_cleanupagent = '%' AND @rpcsrvname = '%' 
  1888.     SELECT       'distributor'           = @loc_distributor,
  1889.                'distribution database' = @loc_distribdb,
  1890.                'directory'              = @loc_directory,
  1891.                'account'               = @loc_account,
  1892.                'min distrib retention' = @loc_mindistretention,
  1893.                'max distrib retention' = @loc_maxdistretention,
  1894.                'history retention'     = @loc_historyretention,
  1895.                'history cleanup agent'  = @loc_historycleanupagent,
  1896.                'distribution cleanup agent' = @loc_distribcleanupagent,
  1897.                'rpc server name' = @loc_rpcsrvname,
  1898.                'rpc login name' = @rpcsrvlogin
  1899.  
  1900.     /*
  1901.     ** Return output parameters if requested.
  1902.     */
  1903.  
  1904.     IF @distributor IS NULL
  1905.         SELECT @distributor = @loc_distributor
  1906.     IF @distribdb IS NULL
  1907.         SELECT @distribdb = @loc_distribdb
  1908.     IF @directory IS NULL
  1909.         SELECT @directory = @loc_directory
  1910.     IF @account IS NULL
  1911.         SELECT @account = @loc_account
  1912.     IF @min_distretention IS NULL
  1913.         SELECT @min_distretention = @loc_mindistretention
  1914.     IF @max_distretention IS NULL
  1915.         SELECT @max_distretention = @loc_maxdistretention
  1916.     IF @history_retention IS NULL
  1917.         SELECT @history_retention = @loc_historyretention
  1918.     IF @history_cleanupagent IS NULL
  1919.         SELECT @history_cleanupagent = @loc_historycleanupagent
  1920.     IF @distrib_cleanupagent IS NULL
  1921.         SELECT @distrib_cleanupagent = @loc_distribcleanupagent
  1922.    
  1923.     IF @rpcsrvname IS NULL
  1924.     begin
  1925.         -- Use local RPC if possible to avoid blocking problem.
  1926.         if is_srvrolemember('sysadmin') = 1 and UPPER(@loc_distributor) = UPPER(@@servername)
  1927.             select @rpcsrvname = @@servername
  1928.         else
  1929.             select @rpcsrvname = @loc_rpcsrvname
  1930.     end
  1931.   
  1932.     RETURN (0)
  1933. GO
  1934.  
  1935. dump tran master with no_log
  1936. go
  1937.  
  1938. raiserror('Creating procedure sp_enumdsn', 0,1)
  1939. GO
  1940.  
  1941. CREATE PROCEDURE sp_enumdsn
  1942.     AS
  1943.  
  1944.     SET NOCOUNT ON
  1945.  
  1946.     DECLARE @distributor sysname
  1947.     DECLARE @distproc nvarchar (300)
  1948.     DECLARE @retcode int
  1949.  
  1950.     DECLARE @dsotype_odbc int
  1951.     DECLARE @dsotype_oledb int
  1952.  
  1953.     select @dsotype_odbc = 1
  1954.     select @dsotype_oledb = 3
  1955.  
  1956.     /*
  1957.     ** Get distribution server information for remote RPC
  1958.     ** subscription calls.
  1959.     */
  1960.  
  1961.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT
  1962.     IF @@error <> 0 OR @retcode <> 0 or @distributor is null
  1963.         BEGIN
  1964.         RAISERROR (14071, 16, -1)
  1965.             RETURN (1)
  1966.     END
  1967.  
  1968.     create table #datasourcestemptable (DataSourceName sysname collate database_default not null, Description nvarchar(255) collate database_default null, DataSourceType int null, ProviderName nvarchar(255) collate database_default null)
  1969.     
  1970.     /*
  1971.     ** Call xp_enumdsn
  1972.     */
  1973.     SELECT @distproc = RTRIM(@distributor) + '.master..xp_enumdsn'
  1974.     insert into #datasourcestemptable(DataSourceName, Description) EXEC @retcode = @distproc
  1975.     IF @@error <> 0
  1976.         BEGIN
  1977.         RAISERROR (14071, 16, -1)
  1978.         RETURN (1)
  1979.     END
  1980.  
  1981.     update #datasourcestemptable set DataSourceType = @dsotype_odbc where DataSourceType is null
  1982.     IF @@error <> 0 
  1983.     BEGIN
  1984.         RETURN (1)
  1985.     END
  1986.  
  1987.     /*
  1988.     ** Call sp_enumoledbdatasources
  1989.     */
  1990.     SELECT @distproc = RTRIM(@distributor) + '.master.dbo.sp_enumoledbdatasources'
  1991.     insert into #datasourcestemptable(DataSourceName, Description, ProviderName) EXEC @retcode = @distproc
  1992.     IF @@error <> 0
  1993.         BEGIN
  1994.         RAISERROR (14071, 16, -1)
  1995.         RETURN (1)
  1996.     END
  1997.  
  1998.     update #datasourcestemptable set DataSourceType = @dsotype_oledb where DataSourceType is null
  1999.     IF @@error <> 0 
  2000.     BEGIN
  2001.         RETURN (1)
  2002.     END
  2003.     
  2004.     select 'Data Source Name' = DataSourceName, Description, 'Type' = DataSourceType, 'Provider Name' = ProviderName 
  2005.     from #datasourcestemptable
  2006.     order by 3, 1
  2007.  
  2008.     drop table #datasourcestemptable
  2009.  
  2010.     return (0)
  2011. go
  2012.  
  2013. raiserror('Creating procedure sp_enumoledbdatasources', 0,1)
  2014. GO
  2015.  
  2016. CREATE PROCEDURE sp_enumoledbdatasources
  2017.     AS
  2018.     set nocount on
  2019.  
  2020.     select srvname, srvproduct, providername from master..sysservers where 
  2021.     (srvstatus & 0x0080) <> 0
  2022.  
  2023.     return (0)
  2024. go
  2025.  
  2026. raiserror('Creating procedure sp_helpsubscriberinfo', 0,1)
  2027. GO
  2028.  
  2029. CREATE PROCEDURE sp_helpsubscriberinfo
  2030.     @subscriber sysname = '%'
  2031.     AS
  2032.  
  2033.     SET NOCOUNT ON
  2034.  
  2035.     DECLARE @distributor sysname
  2036.     DECLARE @distribdb sysname
  2037.     DECLARE @distproc nvarchar (300)
  2038.     DECLARE @retcode int
  2039.     DECLARE @subscriber_bit smallint
  2040.     DECLARE @show_password bit
  2041.  
  2042.     /*
  2043.     ** Security Check
  2044.     */
  2045.  
  2046.     /*
  2047.     ** Initializations.
  2048.     */
  2049.     SELECT @subscriber_bit = 4
  2050.  
  2051.  
  2052.     /*
  2053.     ** Check if subscriber is valid
  2054.     */
  2055.  
  2056.     IF @subscriber IS NULL
  2057.         BEGIN
  2058.             RAISERROR (14043, 16, -1, '@subscriber')
  2059.             RETURN (1)
  2060.         END
  2061.  
  2062.  
  2063.     IF @subscriber <> '%'
  2064.         BEGIN
  2065.  
  2066.             EXECUTE @retcode = dbo.sp_validname @subscriber
  2067.             IF @retcode <> 0
  2068.                 RETURN (1)
  2069.  
  2070.             IF NOT EXISTS (SELECT *
  2071.                         FROM master..sysservers
  2072.                         WHERE UPPER(srvname) = UPPER(@subscriber) collate database_default
  2073.                         AND (srvstatus & @subscriber_bit) <> 0)
  2074.                 BEGIN
  2075.                     RAISERROR (14010, 16, -1)
  2076.                     RETURN (1)
  2077.                 END
  2078.         END
  2079.  
  2080.  
  2081.     /*
  2082.     ** Get distribution server information for remote RPC
  2083.     ** subscription calls.
  2084.     */
  2085.  
  2086.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2087.                                        @distribdb   = @distribdb OUTPUT
  2088.  
  2089.     /* RAISEERROR is subscriber is not '%' */                                               
  2090.     IF @@error <> 0 OR @retcode <> 0 OR @distribdb IS NULL OR @distributor IS NULL
  2091.         BEGIN
  2092.         IF @subscriber <> '%'
  2093.             BEGIN
  2094.                 RAISERROR (14071, 16, -1)
  2095.                 RETURN (1)
  2096.             END
  2097.         ELSE
  2098.             RETURN 0
  2099.         END
  2100.  
  2101.      create table #subscriber_info
  2102.      (
  2103.         publisher  sysname collate database_default not null,
  2104.         subscriber  sysname collate database_default not null,
  2105.         type tinyint not null,           /* 0: MS SQL Server 1: ODBC Data Source */
  2106.         login sysname collate database_default null, 
  2107.         password nvarchar(524) NULL,
  2108.         commit_batch_size int not null,  -- commit_batch_size, no longer supported
  2109.         status_batch_size int not null,  -- status_batch_size, no longer supported
  2110.         flush_frequency int not null,  -- flush_frequency, no longer supported
  2111.         frequency_type int not null,
  2112.         frequency_interval int not null,
  2113.         frequency_relative_interval int not null,
  2114.         frequency_recurrence_factor int not null,
  2115.         frequency_subday int not null,
  2116.         frequency_subday_interval int not null,
  2117.         active_start_time_of_day int not null,
  2118.         active_end_time_of_day int not null,
  2119.         active_start_date int not null,
  2120.         active_end_date int not null,
  2121.         retryattempt    int not null, -- retryattempt, no longer exist
  2122.         retrydelay  int not null,  -- retrydelay, no longer exist
  2123.         description nvarchar(255) collate database_default null,
  2124.         security_mode int not null,
  2125.         frequency_type2 int not null,
  2126.         frequency_interval2 int not null,
  2127.         frequency_relative_interval2 int not null,
  2128.         frequency_recurrence_factor2 int not null,
  2129.         frequency_subday2 int not null,
  2130.         frequency_subday_interval2 int not null,
  2131.         active_start_time_of_day2 int not null,
  2132.         active_end_time_of_day2 int not null,
  2133.         active_start_date2 int not null,
  2134.         active_end_date2 int not null
  2135.     )
  2136.  
  2137.     IF @@error <> 0
  2138.         RETURN (1)
  2139.      
  2140.     /*
  2141.     ** Retrieve MSsubscriber_info
  2142.     */
  2143.     if is_srvrolemember('sysadmin') = 1
  2144.         select @show_password = 1
  2145.     else
  2146.         select @show_password = 0
  2147.  
  2148.     SELECT @distproc = RTRIM(@distributor) + '.' +
  2149.         RTRIM(@distribdb) + '.dbo.sp_MShelp_subscriber_info'
  2150.  
  2151.     insert into #subscriber_info EXEC @retcode = @distproc 
  2152.         @publisher = @@SERVERNAME, 
  2153.         @subscriber = @subscriber,
  2154.         @show_password = @show_password
  2155.  
  2156.     IF @@error <> 0
  2157.         BEGIN
  2158.         RAISERROR (14071, 16, -1)
  2159.         RETURN (1)
  2160.     END
  2161.  
  2162.     IF @retcode <> 0
  2163.         BEGIN
  2164.         RAISERROR (14085, 16, -1)
  2165.         RETURN (1)
  2166.      END
  2167.  
  2168.     -- Filter out subscribers that are not defined locally but left at the distributor
  2169.     -- This will happen if the publisher is cleaned up when the distributor is offline.
  2170.     select info.* from #subscriber_info info, master..sysservers servers where
  2171.         UPPER(info.subscriber) = UPPER(servers.srvname) collate database_default and
  2172.         (servers.srvstatus & @subscriber_bit) <> 0
  2173.  
  2174. go
  2175.  
  2176. raiserror('Creating procedure sp_replica', 0,1)
  2177. GO
  2178.  
  2179. CREATE PROCEDURE sp_replica (
  2180.         @tabname nvarchar(92),     /* The table being replicated */
  2181.         @replicated nvarchar(5)    /* True or false */
  2182.         ) AS
  2183.  
  2184.     SET NOCOUNT ON
  2185.     RAISERROR (21023, 16, -1,'sp_replica')
  2186.     RETURN(1)
  2187. go
  2188.  
  2189.  
  2190. dump tran master with no_log
  2191. go
  2192.  
  2193. raiserror('Creating procedure sp_addpublisher', 0,1)
  2194. GO
  2195.  
  2196. CREATE PROCEDURE sp_addpublisher (
  2197.     @publisher sysname,      /* publisher server name */
  2198.     @type nvarchar (5) = NULL     /* NULL or 'dist' */
  2199.  ) AS
  2200.  
  2201.     SET NOCOUNT ON
  2202.     RAISERROR (21023, 16, -1,'sp_addpublisher')
  2203.     RETURN(1)
  2204. go
  2205.  
  2206. raiserror('Creating procedure sp_addsubscriber', 0,1)
  2207. GO
  2208.  
  2209. CREATE PROCEDURE sp_addsubscriber (
  2210.     @subscriber sysname,
  2211.     @type tinyint = 0,
  2212.     @login sysname = 'sa',
  2213.     @password sysname = NULL,
  2214.     @commit_batch_size int = 100,
  2215.     @status_batch_size int = 100,
  2216.     @flush_frequency int = 0,
  2217.     @frequency_type int = 64,
  2218.     @frequency_interval int = 1,
  2219.     @frequency_relative_interval int = 1,
  2220.     @frequency_recurrence_factor int = 0,
  2221.     @frequency_subday int = 4,
  2222.     @frequency_subday_interval int = 5,
  2223.     @active_start_time_of_day int = 0,
  2224.     @active_end_time_of_day int = 235959,
  2225.     @active_start_date int = 0,
  2226.     @active_end_date int = 99991231,
  2227.     @description nvarchar (255) = NULL,
  2228.     @security_mode int = 1,  /* backward compatible */ /* 0 standard; 1 integrated */
  2229.     @encrypted_password bit = 0
  2230.         ) AS
  2231.  
  2232.     DECLARE @distributor sysname
  2233.     DECLARE @distribdb sysname
  2234.     DECLARE @distproc nvarchar (300)
  2235.     DECLARE @retcode int
  2236.     DECLARE @dsn_subscriber tinyint
  2237.     DECLARE @jet_subscriber tinyint
  2238.     DECLARE @oledb_subscriber tinyint
  2239.     DECLARE @dist_rpcname sysname
  2240.     DECLARE @platform_nt binary
  2241.  
  2242.     -- Defined in sqlrepl.h
  2243.     select @dsn_subscriber = 1    /* Const: subscriber type 'dsn' */ 
  2244.     select @jet_subscriber = 2   
  2245.     select @oledb_subscriber = 3  
  2246.     
  2247.     select @platform_nt = 0x1
  2248.  
  2249.     /* 
  2250.     ** Check if replication components are installed on this server
  2251.     */
  2252.     exec @retcode = dbo.sp_MS_replication_installed
  2253.     if (@retcode <> 1)
  2254.     begin
  2255.         return (1)
  2256.     end
  2257.     
  2258.     /*
  2259.     ** Parameter Check:  @subscriber.
  2260.     ** Check to make sure that the subscriber doesn't already exist, and
  2261.     ** that the name is a valid non-null identifier.
  2262.     */
  2263.  
  2264.     IF @subscriber IS NULL
  2265.         BEGIN
  2266.             RAISERROR (14043, 16, -1, '@subscriber')
  2267.             RETURN (1)
  2268.         END
  2269.  
  2270.     EXECUTE @retcode = dbo.sp_validname @subscriber
  2271.  
  2272.     IF @@ERROR <> 0 OR @retcode <> 0
  2273.     RETURN (1)
  2274.  
  2275.     if LOWER(@subscriber) = 'all'
  2276.         BEGIN
  2277.             RAISERROR (14032, 16, -1, '@subscriber')
  2278.             RETURN (1)
  2279.         END
  2280.  
  2281.     /* 
  2282.     ** Check for invalid security mode
  2283.     */
  2284.     IF @security_mode < 0 OR @security_mode > 1
  2285.         BEGIN
  2286.             RAISERROR(14109, 16, -1)
  2287.             RETURN (1)
  2288.         END
  2289.  
  2290.     IF (UPPER(@subscriber) = UPPER(@@SERVERNAME) and @platform_nt != ( platform() & @platform_nt ) and @security_mode = 1)
  2291.         BEGIN
  2292.             RAISERROR(21038, 16, -1)
  2293.             RETURN (1)
  2294.         END
  2295.  
  2296.     IF EXISTS (SELECT *
  2297.                  FROM master..sysservers
  2298.                 WHERE UPPER(srvname) = UPPER(@subscriber) collate database_default
  2299.                   AND srvstatus & 4 <> 0)
  2300.  
  2301.         BEGIN
  2302.             RAISERROR (14040, 16, -1, @subscriber)
  2303.             RETURN (1)
  2304.         END
  2305.  
  2306.     IF @password = N''
  2307.         select @password = NULL
  2308.  
  2309.     
  2310.     /*
  2311.     **  If no MSsubscriber_info parameters skip RPC code.
  2312.     */
  2313.  
  2314.     IF @frequency_type = -1
  2315.         GOTO ADDSUB
  2316.  
  2317.     /*
  2318.     ** Get distribution server information for remote RPC
  2319.     ** subscription calls.
  2320.     */
  2321.  
  2322.  /*   BEGIN TRAN addsubscriber */
  2323.  
  2324.     EXEC @retcode = dbo.sp_helpdistributor  @distributor = @distributor OUTPUT,
  2325.                                         @rpcsrvname = @dist_rpcname OUTPUT,
  2326.                                         @distribdb   = @distribdb   OUTPUT
  2327.  
  2328.     IF @@error <> 0
  2329.         BEGIN
  2330.          RAISERROR (14071, 16, -1)
  2331.          goto undo
  2332.      END
  2333.  
  2334.     IF @retcode <> 0 OR @distribdb IS NULL OR @distributor IS NULL
  2335.         BEGIN
  2336.             RAISERROR (14071, 16, -1)
  2337.             goto undo
  2338.         END
  2339.  
  2340.  
  2341.     DECLARE @zeroint int
  2342.     SELECT @zeroint = 0
  2343.     /*
  2344.     ** Insert information into MSsubscriber_info
  2345.     */
  2346.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSadd_subscriber_info'
  2347.     EXEC @retcode = @distproc
  2348.          @@SERVERNAME,
  2349.          @subscriber,
  2350.      @type,
  2351.      @login,
  2352.      @password,
  2353.      @commit_batch_size,
  2354.      @status_batch_size,
  2355.      @flush_frequency,
  2356.      @frequency_type,
  2357.      @frequency_interval,
  2358.      @frequency_relative_interval,
  2359.      @frequency_recurrence_factor,
  2360.      @frequency_subday,
  2361.      @frequency_subday_interval,
  2362.      @active_start_time_of_day,
  2363.      @active_end_time_of_day,
  2364.      @active_start_date,
  2365.      @active_end_date,
  2366.      /* Work around of server RPC  named parameter problem */
  2367.      @retryattempts = @zeroint,
  2368.      @retrydelay  = @zeroint,
  2369.      @description = @description,
  2370.      @security_mode = @security_mode,
  2371.      @encrypted_password = @encrypted_password
  2372.  
  2373.     IF @@error <> 0 OR @retcode <> 0
  2374.         BEGIN
  2375.         RAISERROR (14042, 16, -1)
  2376.         goto undo
  2377.     END
  2378.  
  2379.     /*
  2380.     ** add schedule information into MSsubscriber_schedule for merge agent
  2381.     */
  2382.     -- NOTE: We may need better default schedule for merge agent
  2383.     
  2384.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSadd_subscriber_schedule'
  2385.     EXEC @retcode = @distproc
  2386.          @@SERVERNAME,
  2387.          @subscriber,
  2388.          0, -- agent_type = 0 means distribution agent
  2389.          @frequency_type,
  2390.          @frequency_interval,
  2391.          @frequency_relative_interval,
  2392.          @frequency_recurrence_factor,
  2393.          @frequency_subday,
  2394.          @frequency_subday_interval,
  2395.          @active_start_time_of_day,
  2396.          @active_end_time_of_day,
  2397.          @active_start_date,
  2398.          @active_end_date
  2399.     
  2400.     IF @@error <> 0 OR @retcode <> 0
  2401.         BEGIN
  2402.             RAISERROR (14042, 16, -1)
  2403.             goto undo
  2404.         END
  2405.  
  2406.     EXEC @retcode = @distproc
  2407.          @@SERVERNAME,
  2408.          @subscriber,
  2409.          1,             --agent_type = 0 means merge agent
  2410.          4,             --frequency_type,
  2411.          1,             --frequency_interval,
  2412.          1,             --frequency_relative_interval,
  2413.          0,             --frequency_recurrence_factor,
  2414.          8,             --frequency_subday,
  2415.          1,             --frequency_subday_interval,
  2416.          0,             --active_start_time_of_day,
  2417.          235959,        --active_end_time_of_day,
  2418.          0,             --active_start_date,
  2419.          99991231       --active_end_date
  2420.     
  2421.     IF @@error <> 0 OR @retcode <> 0
  2422.         BEGIN
  2423.             RAISERROR (14042, 16, -1)
  2424.             goto undo
  2425.         END
  2426.  
  2427. /*    Commit Transaction addsubscriber */
  2428.  
  2429. ADDSUB:
  2430.  
  2431.     /*
  2432.     ** The server may already be listed in master..sysservers, but might
  2433.     ** not be marked as a subscriber yet.  If it's not in
  2434.     ** master..sysservers, let's add it first.
  2435.     */
  2436.  
  2437.     IF NOT EXISTS (SELECT *
  2438.                          FROM master..sysservers
  2439.                         WHERE LOWER(srvname) = LOWER(@subscriber) collate database_default)
  2440.     begin
  2441.  
  2442.         EXECUTE @retcode = dbo.sp_addserver @subscriber
  2443.  
  2444.             IF @@error <> 0 OR @retcode <> 0
  2445.                 BEGIN
  2446.                 RAISERROR (14042, 16, -1)
  2447.                     RETURN (1)
  2448.                 END
  2449.     end
  2450.     ELSE
  2451.     BEGIN
  2452.         SELECT @subscriber = fn_getpersistedservernamecasevariation(@subscriber) collate database_default
  2453.     END
  2454.  
  2455.     /*
  2456.     ** Set the server option to indicate this is a subscriber.
  2457.     */
  2458.  
  2459.     EXECUTE @retcode = dbo.sp_serveroption @subscriber, 'sub', true
  2460.  
  2461.     IF @@error <> 0 OR @retcode <> 0
  2462.         BEGIN
  2463.            RAISERROR (14042, 16, -1)
  2464.            RETURN (1)
  2465.         END
  2466.  
  2467.     /*
  2468.     ** Set the server option to indicate this is a DSN subscriber.
  2469.     */
  2470.     if @type = @dsn_subscriber OR @type = @jet_subscriber OR @type = @oledb_subscriber
  2471.        BEGIN
  2472.           update master..sysservers set srvproduct = 'MSREPL-NONSQL' where
  2473.             UPPER(srvname) = UPPER(@subscriber) collate database_default
  2474.           IF @@error <> 0 
  2475.             return(1)
  2476.        END
  2477.     
  2478.         RETURN (0) 
  2479. undo:
  2480.  
  2481.     RETURN (1) 
  2482. GO
  2483.  
  2484. raiserror('Creating procedure sp_addsubscriber_schedule', 0,1)
  2485. GO
  2486.  
  2487. CREATE PROCEDURE sp_addsubscriber_schedule (
  2488.     @subscriber sysname,
  2489.     @agent_type smallint = 0,
  2490.     @frequency_type int = 64,
  2491.     @frequency_interval int = 1,
  2492.     @frequency_relative_interval int = 1,
  2493.     @frequency_recurrence_factor int = 0,
  2494.     @frequency_subday int = 4,
  2495.     @frequency_subday_interval int = 5,
  2496.     @active_start_time_of_day int = 0,
  2497.     @active_end_time_of_day int = 235959,
  2498.     @active_start_date int = 0,
  2499.     @active_end_date int = 99991231
  2500.         ) AS
  2501.  
  2502.     DECLARE @distributor sysname
  2503.     DECLARE @distribdb sysname
  2504.     DECLARE @distproc nvarchar (300)
  2505.     DECLARE @retcode int
  2506.     DECLARE @msg nvarchar(255)
  2507.     
  2508.     /*
  2509.     ** Parameter Check:  @subscriber.
  2510.     ** Check to make sure that the subscriber doesn't already exist, and
  2511.     ** that the name is a valid non-null identifier.
  2512.     */
  2513.  
  2514.     IF @subscriber IS NULL
  2515.         BEGIN
  2516.             RAISERROR (14043, 16, -1, '@subscriber')
  2517.             RETURN (1)
  2518.         END
  2519.  
  2520.     EXECUTE @retcode = dbo.sp_validname @subscriber
  2521.  
  2522.     IF @@ERROR <> 0 OR @retcode <> 0
  2523.     RETURN (1)
  2524.  
  2525.     if LOWER(@subscriber) = 'all'
  2526.         BEGIN
  2527.             RAISERROR (14032, 16, -1, '@subscriber')
  2528.             RETURN (1)
  2529.         END
  2530.  
  2531.     IF NOT EXISTS (SELECT * FROM master..sysservers WHERE UPPER(srvname) = UPPER(@subscriber) collate database_default)
  2532.         BEGIN
  2533.             RAISERROR (14048, 16, -1, @subscriber)
  2534.             RETURN (1)
  2535.         END
  2536.     
  2537.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2538.                                        @distribdb   = @distribdb   OUTPUT
  2539.  
  2540.     IF @@error <> 0
  2541.         BEGIN
  2542.          RAISERROR (14071, 16, -1)
  2543.          RETURN (1)
  2544.      END
  2545.  
  2546.     IF @retcode <> 0 OR @distribdb IS NULL OR @distributor IS NULL
  2547.         BEGIN
  2548.             RAISERROR (14071, 16, -1)
  2549.             RETURN (1)
  2550.         END
  2551.     
  2552.     SELECT @distproc = RTRIM(@distributor) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSadd_subscriber_schedule'
  2553.     EXEC @retcode = @distproc
  2554.          @@SERVERNAME,
  2555.          @subscriber,
  2556.          @agent_type,
  2557.          @frequency_type,
  2558.          @frequency_interval,
  2559.          @frequency_relative_interval,
  2560.          @frequency_recurrence_factor,
  2561.          @frequency_subday,
  2562.          @frequency_subday_interval,
  2563.          @active_start_time_of_day,
  2564.          @active_end_time_of_day,
  2565.          @active_start_date,
  2566.          @active_end_date
  2567.     
  2568.     IF @@error <> 0 OR @retcode <> 0
  2569.         BEGIN
  2570.         RAISERROR (14042, 16, -1)
  2571.         RETURN (1)
  2572.     END
  2573.  
  2574.     RETURN (0)
  2575. GO
  2576.  
  2577. raiserror('Creating procedure sp_changesubscriber', 0,1)
  2578. GO
  2579.  
  2580. CREATE PROCEDURE sp_changesubscriber (
  2581.     @subscriber sysname,
  2582.     @type tinyint = NULL,
  2583.     @login sysname = NULL,
  2584.     @password sysname = '%',
  2585.     @commit_batch_size int = NULL,
  2586.     @status_batch_size int = NULL,
  2587.     @flush_frequency int = NULL,
  2588.     @frequency_type int = NULL,
  2589.     @frequency_interval int = NULL,
  2590.     @frequency_relative_interval int = NULL,
  2591.     @frequency_recurrence_factor int = NULL,
  2592.     @frequency_subday int = NULL,
  2593.     @frequency_subday_interval int = NULL,
  2594.     @active_start_time_of_day int = NULL,
  2595.     @active_end_time_of_day int = NULL,
  2596.     @active_start_date int = NULL,
  2597.     @active_end_date int = NULL,
  2598.     @description nvarchar (255) = NULL,
  2599.     @security_mode int = NULL /* 0 standard; 1 integrated */
  2600.  
  2601.         ) AS
  2602.  
  2603.     DECLARE @distributor sysname
  2604.     DECLARE @distribdb sysname
  2605.     DECLARE @distproc nvarchar (300)
  2606.     DECLARE @msg nvarchar(255)
  2607.     DECLARE @retcode int
  2608.     DECLARE @platform_nt binary
  2609.  
  2610.     select @platform_nt = 0x1
  2611.  
  2612.     IF (UPPER(@subscriber) = UPPER(@@SERVERNAME) and @platform_nt != ( platform() & @platform_nt ) and @security_mode = 1)
  2613.         BEGIN
  2614.             RAISERROR(21038, 16, -1)
  2615.             RETURN (1)
  2616.         END
  2617.  
  2618.     /*
  2619.     ** Check to make sure that the subscriber exists.
  2620.     */
  2621.     IF NOT EXISTS (SELECT *
  2622.                  FROM master..sysservers
  2623.                 WHERE UPPER(srvname) = UPPER(@subscriber) collate database_default)
  2624.         BEGIN
  2625.             RAISERROR(14048, 16, 1, @subscriber)
  2626.             RETURN (1)
  2627.         END
  2628.  
  2629.     /*
  2630.     ** Get distribution server information for remote RPC
  2631.     ** subscription calls.
  2632.     */
  2633.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2634.         @distribdb = @distribdb OUTPUT
  2635.     IF @@ERROR <> 0 OR @retcode <> 0
  2636.         BEGIN
  2637.             RAISERROR (14071, 16, -1)
  2638.             RETURN (1)
  2639.         END
  2640.  
  2641.     DECLARE @intnull int
  2642.     /*
  2643.     ** Update MSsubscriber_info
  2644.     */
  2645.     SELECT @distproc = RTRIM(@distributor) + '.' +
  2646.         RTRIM(@distribdb) + '.dbo.sp_MSupdate_subscriber_info'
  2647.     EXEC @retcode = @distproc
  2648.         @@SERVERNAME,
  2649.         @subscriber,
  2650.     @type,
  2651.     @login,
  2652.     @password,
  2653.     @commit_batch_size,
  2654.     @status_batch_size,
  2655.     @flush_frequency,
  2656.     @frequency_type,
  2657.     @frequency_interval,
  2658.     @frequency_relative_interval,
  2659.     @frequency_recurrence_factor,
  2660.     @frequency_subday,
  2661.     @frequency_subday_interval,
  2662.     @active_start_time_of_day,
  2663.     @active_end_time_of_day,
  2664.     @active_start_date,
  2665.     @active_end_date,
  2666.     @retryattempts = @intnull,
  2667.     @retrydelay  = @intnull,
  2668.     @description = @description,
  2669.     @security_mode = @security_mode
  2670.     IF @@ERROR <> 0 OR @retcode <> 0
  2671.         BEGIN
  2672.             RAISERROR (14048, 16, -1, @subscriber)
  2673.             RETURN (1)
  2674.     END
  2675. go
  2676.  
  2677. raiserror('Creating procedure sp_changesubscriber_schedule', 0,1)
  2678. GO
  2679.  
  2680. CREATE PROCEDURE sp_changesubscriber_schedule (
  2681.     @subscriber sysname,
  2682.     @agent_type smallint,
  2683.     @frequency_type int = NULL,
  2684.     @frequency_interval int = NULL,
  2685.     @frequency_relative_interval int = NULL,
  2686.     @frequency_recurrence_factor int = NULL,
  2687.     @frequency_subday int = NULL,
  2688.     @frequency_subday_interval int = NULL,
  2689.     @active_start_time_of_day int = NULL,
  2690.     @active_end_time_of_day int = NULL,
  2691.     @active_start_date int = NULL,
  2692.     @active_end_date int = NULL
  2693.         ) AS
  2694.  
  2695.     DECLARE @distributor sysname
  2696.     DECLARE @distribdb sysname
  2697.     DECLARE @distproc nvarchar (300)
  2698.     DECLARE @msg nvarchar(255)
  2699.     DECLARE @retcode int
  2700.  
  2701.     /*
  2702.     ** Check to make sure that the subscriber DOES exist.
  2703.     */
  2704.     IF NOT EXISTS (SELECT *
  2705.                  FROM master..sysservers
  2706.                 WHERE UPPER(srvname) = UPPER(@subscriber) collate database_default)
  2707.         BEGIN
  2708.             RAISERROR (14048, 16, -1, @subscriber)
  2709.             RETURN (1)
  2710.         END
  2711.  
  2712.     /*
  2713.     ** Get distribution server information for remote RPC
  2714.     ** subscription calls.
  2715.     */
  2716.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2717.         @distribdb = @distribdb OUTPUT
  2718.     IF @@ERROR <> 0 OR @retcode <> 0
  2719.          BEGIN
  2720.             RAISERROR (14071, 16, -1)
  2721.              RETURN (1)
  2722.          END
  2723.  
  2724.     /*
  2725.     ** Update MSsubscriber_info
  2726.     */
  2727.     SELECT @distproc = RTRIM(@distributor) + '.' +
  2728.         RTRIM(@distribdb) + '.dbo.sp_MSupdate_subscriber_schedule'
  2729.     EXEC @retcode = @distproc
  2730.         @@SERVERNAME,
  2731.         @subscriber,
  2732.         @agent_type,
  2733.         @frequency_type,
  2734.         @frequency_interval,
  2735.         @frequency_relative_interval,
  2736.         @frequency_recurrence_factor,
  2737.         @frequency_subday,
  2738.         @frequency_subday_interval,
  2739.         @active_start_time_of_day,
  2740.         @active_end_time_of_day,
  2741.         @active_start_date,
  2742.         @active_end_date
  2743.     
  2744.     IF @@ERROR <> 0 OR @retcode <> 0
  2745.         BEGIN
  2746.             RAISERROR (14048, 16, -1, @subscriber)
  2747.             RETURN (1)
  2748.         END
  2749.         
  2750.     RETURN (0)  
  2751. go 
  2752.     
  2753. raiserror('Creating procedure sp_distcounters', 0,1)
  2754. GO
  2755.  
  2756. CREATE PROCEDURE sp_distcounters
  2757.     AS
  2758.  
  2759.     SET NOCOUNT ON
  2760.  
  2761.     /*
  2762.     ** Declarations.
  2763.     */
  2764.     DECLARE @distributor sysname
  2765.     DECLARE @distribdb sysname
  2766.     DECLARE @distproc nvarchar (300)
  2767.     DECLARE @retcode int
  2768.  
  2769.     /*
  2770.     ** Get distribution server information for remote RPC
  2771.     ** subscription calls.  If no distribution information, assume
  2772.     ** replication is not being used.
  2773.     */
  2774.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  2775.                                        @distribdb = @distribdb OUTPUT
  2776.     IF @@error <> 0 OR @retcode <> 0 OR @distributor IS NULL OR
  2777.        @distribdb IS NULL
  2778.     RETURN (1)
  2779.  
  2780.     /*
  2781.     ** Request counters from Distribution Server
  2782.     */
  2783.     SELECT @distproc = RTRIM(@distributor) + '.' + RTRIM(@distribdb) +
  2784.     '.dbo.sp_MSdistribution_counters'
  2785.     EXEC @retcode = @distproc @@SERVERNAME
  2786. go
  2787.  
  2788.  
  2789. raiserror('Creating procedure sp_droppublisher', 0,1)
  2790. GO
  2791.  
  2792. CREATE PROCEDURE sp_droppublisher (
  2793.     @publisher sysname,         /* publisher server name */
  2794.     @type nvarchar (5) = NULL      /* NULL or 'dist' */
  2795.         ) AS
  2796.  
  2797.     SET NOCOUNT ON
  2798.     RAISERROR (21023, 16, -1,'sp_droppublisher')
  2799.     RETURN(1)
  2800.  
  2801. go
  2802.  
  2803. raiserror('Creating procedure sp_dropsubscriber', 0,1)
  2804. GO
  2805.  
  2806. CREATE PROCEDURE sp_dropsubscriber (
  2807.     @subscriber sysname,        /* The name of the subscriber */
  2808.     @reserved nvarchar(50) = NULL,
  2809.     @ignore_distributor bit = 0
  2810.         ) AS
  2811.  
  2812.     SET NOCOUNT ON
  2813.  
  2814.     /*
  2815.     ** Declarations.
  2816.     */
  2817.  
  2818.     DECLARE @db_name sysname
  2819.     DECLARE @foundSubscription int
  2820.     DECLARE @command nvarchar(255)
  2821.     DECLARE @transpublishdb_bit int
  2822.     DECLARE @mergepublishdb_bit int
  2823.     DECLARE @distributor sysname
  2824.     DECLARE @distribdb sysname
  2825.     DECLARE @distproc nvarchar (300)
  2826.     DECLARE @retcode int
  2827.     DECLARE @type nvarchar(10)
  2828.  
  2829.     SELECT @transpublishdb_bit = 1
  2830.     SELECT @mergepublishdb_bit = 4
  2831.  
  2832.  
  2833.     /*
  2834.     ** Parameter Check:  @subscriber.
  2835.     ** Check to make sure that the subscriber exists.
  2836.     */
  2837.  
  2838.     IF @subscriber IS NULL
  2839.         BEGIN
  2840.             RAISERROR (14043, 16, -1, '@subscriber')
  2841.             RETURN (1)
  2842.         END
  2843.  
  2844.     EXECUTE @retcode = dbo.sp_validname @subscriber
  2845.  
  2846.     IF @retcode <> 0
  2847.     RETURN (1)
  2848.  
  2849.     IF lower(@subscriber) <> 'all' and NOT EXISTS (SELECT *
  2850.                      FROM master..sysservers
  2851.                     WHERE UPPER(srvname) = UPPER(@subscriber) collate database_default
  2852.                       AND srvstatus & 4 <> 0)
  2853.  
  2854.         BEGIN
  2855.             RAISERROR (14048, 16, -1, @subscriber)
  2856.             RETURN (1)
  2857.         END
  2858.  
  2859.     if lower(@subscriber) = 'all'
  2860.     begin
  2861.         DECLARE hCdropsubscriber_all CURSOR LOCAL FAST_FORWARD FOR
  2862.             SELECT srvname
  2863.                      FROM master..sysservers
  2864.                     WHERE srvstatus & 4 <> 0
  2865.         FOR READ ONLY
  2866.         OPEN hCdropsubscriber_all
  2867.         FETCH hCdropsubscriber_all INTO @subscriber
  2868.     
  2869.         WHILE (@@fetch_status <> -1)
  2870.         BEGIN
  2871.             EXECUTE @retcode  = dbo.sp_dropsubscriber
  2872.                 @subscriber = @subscriber,    
  2873.                 @ignore_distributor = @ignore_distributor,
  2874.                 @reserved = @reserved
  2875.             IF @@ERROR <> 0 OR @retcode <> 0
  2876.             BEGIN
  2877.                 CLOSE hCdropsubscriber_all
  2878.                 DEALLOCATE hCdropsubscriber_all
  2879.                 RETURN (1)
  2880.             END
  2881.             FETCH hCdropsubscriber_all INTO @subscriber
  2882.         end
  2883.         
  2884.         CLOSE hCdropsubscriber_all
  2885.         DEALLOCATE hCdropsubscriber_all
  2886.  
  2887.         return(0)
  2888.     end
  2889.  
  2890.  
  2891.     /*
  2892.     ** There should be no subscription by the subscriber
  2893.     ** Open a cursor the published databases.
  2894.     **
  2895.     */
  2896.     DECLARE hCdropsubscriber CURSOR LOCAL FAST_FORWARD FOR
  2897.             SELECT name, N'tran' FROM master..sysdatabases      
  2898.                 WHERE (category & @transpublishdb_bit) <> 0 
  2899.         UNION
  2900.             select name, N'merge' from master..sysdatabases      
  2901.                 WHERE (category & @mergepublishdb_bit) <> 0 
  2902.     FOR READ ONLY
  2903.  
  2904.     OPEN hCdropsubscriber
  2905.     FETCH hCdropsubscriber INTO @db_name, @type
  2906.     
  2907.     WHILE (@@fetch_status <> -1)
  2908.     BEGIN        
  2909.         IF LOWER(@reserved collate SQL_Latin1_General_CP1_CS_AS) = 'drop_subscriptions'
  2910.         BEGIN
  2911.             if @type = 'tran'
  2912.             begin
  2913.                 SELECT @command = quotename(@db_name) + '.dbo.sp_dropsubscription'
  2914.                 EXECUTE @retcode  = @command 
  2915.                     @publication = 'all', 
  2916.                     @article = 'all', 
  2917.                     @subscriber = @subscriber,
  2918.                     @ignore_distributor = @ignore_distributor
  2919.             end
  2920.             if @type = 'merge'
  2921.             begin
  2922.                 SELECT @command = quotename(@db_name) + '.dbo.sp_dropmergesubscription'
  2923.                 EXECUTE @retcode  = @command 
  2924.                     @publication = 'all', 
  2925.                     @subscriber = @subscriber,
  2926.                     @subscription_type = 'both',
  2927.                     @ignore_distributor = @ignore_distributor
  2928.             end
  2929.             IF @@ERROR <> 0 OR @retcode <> 0
  2930.             BEGIN
  2931.                 CLOSE hCdropsubscriber
  2932.                 DEALLOCATE hCdropsubscriber
  2933.                 RETURN (1)
  2934.             END
  2935.         END
  2936.         
  2937.         -- Set @foundSubscription to not null so that sp_helpsubscription will not display
  2938.         -- result set
  2939.         select @foundSubscription = 0
  2940.  
  2941.         if @type = 'tran'
  2942.         begin
  2943.             SELECT @command = quotename(@db_name) + '.dbo.sp_helpsubscription'
  2944.             EXECUTE @retcode  = @command @publication = '%', @article = '%', 
  2945.                 @subscriber = @subscriber,
  2946.                 @found = @foundSubscription  OUTPUT
  2947.  
  2948.             IF @@ERROR <> 0 OR @retcode <> 0
  2949.             BEGIN
  2950.                 CLOSE hCdropsubscriber
  2951.                 DEALLOCATE hCdropsubscriber
  2952.                 RETURN (1)
  2953.             END
  2954.         end
  2955.         else
  2956.         begin
  2957.             SELECT @command = quotename(@db_name) + '.dbo.sp_helpmergesubscription'
  2958.             EXECUTE @retcode  = @command @publication = '%', 
  2959.                 @subscriber = @subscriber,
  2960.                 @subscription_type = 'both',
  2961.                 @found = @foundSubscription  OUTPUT
  2962.  
  2963.             IF @@ERROR <> 0 OR @retcode <> 0
  2964.             BEGIN
  2965.                 CLOSE hCdropsubscriber
  2966.                 DEALLOCATE hCdropsubscriber
  2967.                 RETURN (1)
  2968.             END
  2969.         end
  2970.  
  2971.  
  2972.         IF @foundSubscription <> 0
  2973.         BEGIN
  2974.             CLOSE hCdropsubscriber
  2975.             DEALLOCATE hCdropsubscriber
  2976.             RAISERROR ( 14144,  16, -1, @subscriber, @db_name)
  2977.             RETURN (1)
  2978.         END
  2979.  
  2980.         FETCH hCdropsubscriber INTO @db_name, @type
  2981.  
  2982.     END
  2983.     
  2984.     CLOSE hCdropsubscriber
  2985.     DEALLOCATE hCdropsubscriber
  2986.  
  2987.     /*
  2988.     ** Drop the subsubscriber_info in the distribution database
  2989.     */
  2990.     /*
  2991.     ** if @ignore_distributor = 1, we are in bruteforce cleanup mode, don't do RPC.
  2992.     */
  2993.     if @ignore_distributor = 0
  2994.     begin
  2995.  
  2996.  
  2997.         /*
  2998.         ** Get distribution server information for remote RPC
  2999.         ** agent verification.
  3000.         */
  3001.         EXEC @retcode = dbo.sp_helpdistributor 
  3002.             @rpcsrvname = @distributor OUTPUT,
  3003.             @distribdb = @distribdb OUTPUT
  3004.         IF @@error <> 0 OR @retcode <> 0
  3005.             BEGIN
  3006.             RAISERROR (14071, 16, -1)
  3007.                RETURN (1)
  3008.         END
  3009.         
  3010.         /*
  3011.         ** Insert information into MSsubscriber_info in the distribution db
  3012.         */
  3013.         if @distribdb is not null
  3014.         begin
  3015.             SELECT @distproc = RTRIM(@distributor) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSdrop_subscriber_info'
  3016.             EXEC @retcode = @distproc
  3017.                  @publisher = @@SERVERNAME,
  3018.                  @subscriber = @subscriber
  3019.  
  3020.             IF @@error <> 0 OR @retcode <> 0
  3021.                 BEGIN
  3022.                 RAISERROR (14042, 16, -1)
  3023.                 RETURN (1)
  3024.             END
  3025.         end
  3026.     end
  3027.  
  3028.     -- We have already validated the existence of the server at this point
  3029.     SELECT @subscriber = fn_getpersistedservernamecasevariation(@subscriber) collate database_default
  3030.  
  3031.     /*
  3032.     ** Turn off the subscriber server option.
  3033.     */
  3034.     EXECUTE @retcode = dbo.sp_serveroption @subscriber, 'sub', false
  3035.     IF @@ERROR <> 0 OR @retcode <> 0
  3036.     BEGIN
  3037.        RAISERROR (14047, 16, -1, @subscriber)
  3038.        RETURN (1)
  3039.     END
  3040.  
  3041.     RAISERROR (14062, 10, -1)
  3042. go
  3043.  
  3044. raiserror('Creating procedure sp_dsninfo', 0,1)
  3045. GO
  3046.  
  3047. CREATE PROCEDURE sp_dsninfo
  3048. --  xp_dsninfo does not support unicode
  3049.     @dsn varchar(128),
  3050.     @infotype varchar(128) = NULL,
  3051.     @login varchar(128) = NULL,
  3052.     @password varchar(128) = NULL,
  3053.     @dso_type int = 1  /* 1 is ODBC, 3 OLEDB. */
  3054.     AS
  3055.  
  3056.     SET NOCOUNT ON
  3057.  
  3058.     DECLARE @distributor sysname
  3059.     DECLARE @distproc nvarchar (300)
  3060.     DECLARE @retcode int
  3061.     DECLARE @dsotype_odbc int
  3062.     DECLARE @dsotype_oledb int
  3063.  
  3064.     select @dsotype_odbc = 1
  3065.     select @dsotype_oledb = 3
  3066.  
  3067.     /*
  3068.     ** Get distribution server information for remote RPC
  3069.     ** subscription calls.
  3070.     */
  3071.  
  3072.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT
  3073.     IF @@error <> 0 OR @retcode <> 0
  3074.         BEGIN
  3075.         RAISERROR (14071, 16, -1)
  3076.             RETURN (1)
  3077.     END
  3078.  
  3079.     if (@dso_type = @dsotype_odbc)
  3080.     begin
  3081.         /*
  3082.         ** Call xp_dsninfo
  3083.         */
  3084.         SELECT @distproc = RTRIM(@distributor) + '.master..xp_dsninfo'
  3085.         EXEC @retcode = @distproc @dsn, @infotype, @login, @password
  3086.         IF @@error <> 0
  3087.             BEGIN
  3088.             RAISERROR (14071, 16, -1)
  3089.             RETURN (1)
  3090.         END
  3091.     end
  3092.     else if (@dso_type = @dsotype_oledb)
  3093.     begin
  3094.         /*
  3095.         ** Call sp_oledbinfo
  3096.         */
  3097.         EXEC @retcode = master.dbo.sp_oledbinfo @dsn, @infotype, @login, @password
  3098.         IF @@error <> 0
  3099.             BEGIN
  3100.             RAISERROR (14071, 16, -1)
  3101.             RETURN (1)
  3102.         END
  3103.     end
  3104.  
  3105. go
  3106.  
  3107. dump tran master with no_log
  3108. go
  3109.  
  3110. raiserror('Creating procedure sp_publishdb', 0,1)
  3111. GO
  3112.  
  3113. /* For backward compatible */
  3114. CREATE PROCEDURE sp_publishdb @dbname sysname,@value nvarchar (5)
  3115.     AS
  3116.  
  3117.     DECLARE @retcode int
  3118.  
  3119.     EXECUTE @retcode = dbo.sp_replicationdboption @dbname, 'publish', @value
  3120.  
  3121.     IF @@ERROR <> 0 or @retcode <> 0
  3122.     BEGIN
  3123.        RETURN (1)
  3124.     END
  3125.  
  3126.     RETURN(0)
  3127. go
  3128.  
  3129.  
  3130. raiserror('Creating procedure sp_MScreate_dist_tables', 0,1)
  3131. GO
  3132.  
  3133. CREATE PROCEDURE sp_MScreate_dist_tables
  3134. AS
  3135.  
  3136.    /*
  3137.    ** Important:  
  3138.    ** We use varbinary(16) for xact_id and xact_seqno, we don't want ending nulls
  3139.    ** to be truncated by the server
  3140.    **
  3141.    ** Also, in MSrepl_commands, we don't want ending space to be truncated.  
  3142.    */
  3143.    SET ANSI_PADDING ON
  3144.  
  3145.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_version' and type = 'U')
  3146.    BEGIN
  3147.       /****************************************************************************/
  3148.       raiserror('Creating table MSrepl_version', 0,1)
  3149.       /****************************************************************************/
  3150.       CREATE TABLE dbo.MSrepl_version
  3151.       (
  3152.       major_version int NOT NULL,
  3153.       minor_version int NOT NULL,
  3154.       revision int NOT NULL,
  3155.       db_existed bit NULL
  3156.       )
  3157.       CREATE UNIQUE CLUSTERED INDEX ucMSrepl_version ON dbo.MSrepl_version
  3158.                     (major_version, minor_version, revision)
  3159.     
  3160.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_version'
  3161.       
  3162.       INSERT INTO MSrepl_version VALUES (7,0,0,0)
  3163.    END
  3164.  
  3165.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSpublisher_databases' and type = 'U')
  3166.    BEGIN
  3167.       /****************************************************************************/
  3168.       raiserror('Creating table MSpublisher_databases', 0,1)
  3169.       /****************************************************************************/
  3170.       CREATE TABLE dbo.MSpublisher_databases(
  3171.         publisher_id smallint NOT NULL,
  3172.         publisher_db sysname NULL,
  3173.         id int identity NOT NULL,                   
  3174.     )
  3175.       EXEC dbo.sp_MS_marksystemobject 'MSpublisher_databases'
  3176.  
  3177.       raiserror('Creating clustered index ucMSpublisher_databases', 0,1)
  3178.       CREATE UNIQUE CLUSTERED INDEX ucMSpublisher_databases ON dbo.MSpublisher_databases
  3179.      (publisher_id, publisher_db, id)
  3180.    END
  3181.  
  3182.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSpublications' and type = 'U')
  3183.    BEGIN
  3184.       /****************************************************************************/
  3185.       raiserror('Creating table MSpublications', 0,1)
  3186.       /****************************************************************************/
  3187.       CREATE TABLE dbo.MSpublications (
  3188.         publisher_id smallint NOT NULL,
  3189.         publisher_db sysname NULL,
  3190.         publication sysname NOT NULL,
  3191.         publication_id int identity NOT NULL,   -- This id IS NOT the same as the SQL Server publisher's
  3192.         publication_type int NOT NULL,          -- 0 = Snapshot 1 = Transactional
  3193.         thirdparty_flag bit NOT NULL,           -- 0 = SQL Server 1 = Third Party
  3194.         independent_agent bit NOT NULL,
  3195.         immediate_sync bit NOT NULL,
  3196.         allow_push bit NOT NULL,
  3197.         allow_pull bit NOT NULL,
  3198.         allow_anonymous bit NOT NULL,
  3199.         description nvarchar(255) NULL,
  3200.         vendor_name nvarchar(100) NULL,
  3201.         retention    int NULL,
  3202.         sync_method int default 0 NOT NULL,
  3203.         allow_subscription_copy bit default 0 not null,
  3204.         thirdparty_options int null,
  3205.         allow_queued_tran bit default 0    not null
  3206.         )
  3207.       EXEC dbo.sp_MS_marksystemobject 'MSpublications'
  3208.  
  3209.      -- publication_id needs to be the first columns in the index. It
  3210.      -- is used in sp_MSmaximum_cleanup_seqno.
  3211.       raiserror('Creating clustered index ucMSpublications', 0,1)
  3212.       CREATE UNIQUE CLUSTERED INDEX ucMSpublications ON dbo.MSpublications
  3213.      (publication_id,  publication, publisher_db, publisher_id)
  3214.  
  3215.    END
  3216.    ELSE
  3217.    BEGIN
  3218.       IF NOT EXISTS (select * from syscolumns
  3219.         where name = 'sync_method'
  3220.         and id=object_id('MSpublications'))
  3221.       BEGIN
  3222.          ALTER TABLE MSpublications ADD sync_method int default 0 not null
  3223.       END
  3224.  
  3225.       IF NOT EXISTS (select * from syscolumns
  3226.         where name = 'allow_subscription_copy'
  3227.         and id=object_id('MSpublications'))
  3228.       BEGIN
  3229.          ALTER TABLE MSpublications ADD allow_subscription_copy bit default 0 not null
  3230.       END
  3231.       IF NOT EXISTS (select * from syscolumns
  3232.         where name = 'thirdparty_options'
  3233.         and id=object_id('MSpublications'))
  3234.       BEGIN
  3235.          ALTER TABLE MSpublications ADD thirdparty_options int null
  3236.       END
  3237.  
  3238.       IF NOT EXISTS (select * from syscolumns
  3239.         where name = 'allow_queued_tran'
  3240.         and id=object_id('MSpublications'))
  3241.       BEGIN
  3242.          ALTER TABLE MSpublications ADD allow_queued_tran bit default 0    not null
  3243.       END
  3244.    END
  3245.  
  3246.  
  3247.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSarticles' and type = 'U')
  3248.    BEGIN
  3249.       /****************************************************************************/
  3250.       raiserror('Creating table MSarticles', 0,1)
  3251.       /****************************************************************************/
  3252.     CREATE TABLE dbo.MSarticles (
  3253.         publisher_id smallint NOT NULL,
  3254.         publisher_db sysname NULL,
  3255.         publication_id int NOT NULL,
  3256.         article sysname NOT NULL,
  3257.         article_id int NOT NULL,                            -- This id is the same as a SQL Server Publisher's
  3258.         destination_object sysname NULL,
  3259.         source_owner    sysname NULL,
  3260.         source_object sysname NULL,
  3261.         description nvarchar(255) NULL
  3262.     )   
  3263.  
  3264.       EXEC dbo.sp_MS_marksystemobject 'MSarticles'
  3265.  
  3266.       raiserror('Creating clustered index ucMSarticles', 0,1)
  3267.       CREATE UNIQUE CLUSTERED INDEX ucMSarticles ON dbo.MSarticles
  3268.      (publisher_db, publisher_id, article_id, article, publication_id)
  3269.    END
  3270.  
  3271.     IF NOT EXISTS( SELECT * FROM sysobjects WHERE name = 'MSsync_states' )
  3272.     BEGIN
  3273.         CREATE TABLE dbo.MSsync_states
  3274.         (
  3275.             publisher_id smallint not null,
  3276.             publisher_db sysname not null,
  3277.             publication_id int not null 
  3278.         )
  3279.         EXEC dbo.sp_MS_marksystemobject 'MSsync_states'
  3280.  
  3281.         CREATE UNIQUE CLUSTERED INDEX ucMSsyncstates on dbo.MSsync_states
  3282.         ( publisher_id, publisher_db, publication_id )
  3283.     END
  3284.  
  3285.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsubscriptions' and type = 'U')
  3286.    BEGIN
  3287.       /****************************************************************************/
  3288.       raiserror('Creating table MSsubscriptions', 0,1)
  3289.       /****************************************************************************/
  3290.        CREATE TABLE dbo.MSsubscriptions
  3291.       (
  3292.         publisher_database_id int NOT NULL,         -- Used to reference MSrepl_transactions and MSrepl_commands
  3293.         publisher_id smallint NOT NULL,
  3294.         publisher_db sysname NOT NULL,
  3295.         publication_id int NOT NULL,
  3296.         article_id int NOT NULL,
  3297.         subscriber_id smallint NOT NULL,
  3298.         subscriber_db sysname NOT NULL,
  3299.         subscription_type int NOT NULL,         -- 0 = push, 1 = pull, 2 = anonymous 
  3300.         sync_type tinyint NOT NULL,                 -- 1 = automatic 2 = no sync
  3301.         status tinyint NOT NULL,                        -- 0 = inactive, 1 = subscribed, 2 = active 
  3302.         subscription_seqno varbinary(16) NOT NULL,  -- publisher's database sequence number 
  3303.         snapshot_seqno_flag bit NOT NULL,               -- 1 if subscription_seqno is the snapshot seqno
  3304.         independent_agent bit NOT NULL,             -- Value carried over from MSpublications
  3305.         subscription_time datetime NOT NULL, 
  3306.         loopback_detection bit NOT NULL,  
  3307.         agent_id int NOT NULL,
  3308.         update_mode tinyint NOT NULL,                -- 0 = read only, 1 = sync tran, 2 = queued tran, 
  3309.                                                     -- 3 = failover, 4 = sqlqueued tran, 5 = sqlqueued failover
  3310.         publisher_seqno varbinary(16) NOT NULL,
  3311.         ss_cplt_seqno   varbinary(16) NOT NULL
  3312.       )
  3313.  
  3314.       EXEC dbo.sp_MS_marksystemobject 'MSsubscriptions'
  3315.  
  3316.       raiserror('Creating clustered index ucMSsubscirptions', 0,1)
  3317.       CREATE UNIQUE CLUSTERED INDEX ucMSsubscriptions ON dbo.MSsubscriptions
  3318.       (agent_id, article_id)
  3319.  
  3320.       -- Index used by sp_MSdelete_publisherdb_trans
  3321.       raiserror('Creating index iMSsubscriptions', 0,1)
  3322.       CREATE INDEX iMSsubscriptions ON dbo.MSsubscriptions
  3323.       (publisher_database_id, article_id, subscriber_id, subscriber_db, publication_id, publisher_db, publisher_id)
  3324.     END
  3325.     ELSE
  3326.     BEGIN
  3327.         if not exists ( select * from syscolumns 
  3328.                         where id = object_id( N'MSsubscriptions' ) 
  3329.                         and name = N'ss_cplt_seqno' )
  3330.         begin                
  3331.             exec ('alter table MSsubscriptions add ss_cplt_seqno varbinary(16) NULL')
  3332.             exec ('update MSsubscriptions set ss_cplt_seqno = publisher_seqno')
  3333.             exec ('alter table MSsubscriptions alter column ss_cplt_seqno varbinary(16) NOT NULL')
  3334.         end
  3335.     END
  3336.  
  3337.    -- For beta 3 upgrade, we need to create new index here.
  3338.    if not exists (select * from sysindexes where name = 'iMSsubscriptions2')
  3339.    begin
  3340.       -- Index used by sp_MSdelete_publisherdb_trans
  3341.       raiserror('Creating index iMSsubscriptions2', 0,1)
  3342.       CREATE INDEX iMSsubscriptions2 ON dbo.MSsubscriptions
  3343.       (publisher_database_id, subscription_seqno)
  3344.    end
  3345.  
  3346.    
  3347.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSmerge_subscriptions' and type = 'U')
  3348.    BEGIN
  3349.       /****************************************************************************/
  3350.       raiserror('Creating table MSmerge_subscriptions', 0,1)
  3351.       /****************************************************************************/
  3352.        CREATE TABLE dbo.MSmerge_subscriptions
  3353.       (
  3354.         publisher_id smallint NOT NULL,
  3355.         publisher_db sysname NULL,
  3356.         publication_id int NOT NULL,
  3357.         subscriber_id smallint NOT NULL,
  3358.         subscriber_db sysname NULL,
  3359.         subscription_type int NULL,         -- 0 = push, 1 = pull, 2 = anonymous 
  3360.         sync_type tinyint NOT NULL,                 -- 1 = automatic 2 = no sync
  3361.         status tinyint NOT NULL,                        -- 0 = inactive, 1 = subscribed, 2 = active 
  3362.         subscription_time datetime NOT NULL
  3363.       )
  3364.  
  3365.       EXEC dbo.sp_MS_marksystemobject 'MSmerge_subscriptions'
  3366.  
  3367.       raiserror('Creating clustered index ucMSmerge_subscriptions', 0,1)
  3368.       CREATE UNIQUE CLUSTERED INDEX ucMSmerge_subscriptions ON dbo.MSmerge_subscriptions
  3369.      (publisher_id, publisher_db, publication_id, subscriber_id, subscriber_db)
  3370.    END
  3371.  
  3372.  
  3373.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_transactions' and type = 'U')
  3374.    BEGIN
  3375.       /****************************************************************************/
  3376.       raiserror('Creating table MSrepl_transactions', 0,1)
  3377.       /****************************************************************************/
  3378.       CREATE TABLE dbo.MSrepl_transactions
  3379.       (
  3380.       publisher_database_id int NOT NULL,
  3381.       xact_id varbinary(16) NULL,
  3382.       xact_seqno varbinary (16 )  NOT NULL,
  3383.       entry_time datetime  NOT NULL
  3384.       )
  3385.  
  3386.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_transactions'
  3387.  
  3388.       raiserror('Creating clustered index usMSrepl_transactions', 0,1)
  3389.       CREATE UNIQUE CLUSTERED INDEX ucMSrepl_transactions ON dbo.MSrepl_transactions
  3390.          (publisher_database_id, xact_seqno)
  3391.          WITH STATISTICS_NORECOMPUTE
  3392.  
  3393.       CREATE STATISTICS stat_publisher_database_id
  3394.          ON MSrepl_transactions (publisher_database_id)
  3395.          WITH NORECOMPUTE
  3396.  
  3397.       CREATE STATISTICS stat_xact_id
  3398.          ON MSrepl_transactions (xact_id)
  3399.          WITH NORECOMPUTE
  3400.  
  3401.       CREATE STATISTICS stat_xact_seqno
  3402.          ON MSrepl_transactions (xact_seqno)
  3403.          WITH NORECOMPUTE
  3404.  
  3405.       CREATE STATISTICS stat_entry_time
  3406.          ON MSrepl_transactions (entry_time)
  3407.          WITH NORECOMPUTE
  3408.  
  3409.    END
  3410.  
  3411.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_commands' and type = 'U')
  3412.    BEGIN
  3413.  
  3414.       /****************************************************************************/
  3415.       raiserror('Creating table MSrepl_commands', 0,1)
  3416.       /****************************************************************************/
  3417.  
  3418.  
  3419.       CREATE TABLE dbo.MSrepl_commands (
  3420.       publisher_database_id int not null,
  3421.       xact_seqno varbinary(16) not null,
  3422.       type int not null, 
  3423.       article_id int not null,
  3424.       originator_id int not null,
  3425.       command_id int not null,
  3426.       partial_command bit not null,
  3427.       command varbinary(1024) NULL
  3428.       )
  3429.  
  3430.  
  3431.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_commands'
  3432.  
  3433.       raiserror('Creating clusterd index ucMSrepl_commands', 0,1)
  3434.       CREATE UNIQUE CLUSTERED INDEX ucMSrepl_commands ON dbo.MSrepl_commands
  3435.          (publisher_database_id, xact_seqno, command_id)
  3436.          WITH STATISTICS_NORECOMPUTE
  3437.  
  3438.       CREATE STATISTICS stat_xact_seqno
  3439.          ON MSrepl_commands (xact_seqno)
  3440.          WITH NORECOMPUTE
  3441.  
  3442.       CREATE STATISTICS stat_type
  3443.          ON MSrepl_commands (type)
  3444.          WITH NORECOMPUTE
  3445.  
  3446.       CREATE STATISTICS stat_article_id
  3447.          ON MSrepl_commands (article_id)
  3448.          WITH NORECOMPUTE
  3449.  
  3450.       CREATE STATISTICS stat_originator_id
  3451.          ON MSrepl_commands (originator_id)
  3452.          WITH NORECOMPUTE
  3453.  
  3454.       CREATE STATISTICS stat_command_id
  3455.          ON MSrepl_commands (command_id)
  3456.          WITH NORECOMPUTE
  3457.  
  3458.       CREATE STATISTICS stat_partial_command
  3459.          ON MSrepl_commands (partial_command)
  3460.          WITH NORECOMPUTE
  3461.  
  3462.    END
  3463.  
  3464.  
  3465.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_originators' and type = 'U')
  3466.    BEGIN
  3467.       /****************************************************************************/
  3468.       raiserror('Creating table MSrepl_orginators', 0,1)
  3469.       /****************************************************************************/
  3470.       CREATE TABLE dbo.MSrepl_originators
  3471.       (
  3472.       id int identity not null,
  3473.       publisher_database_id int not null,
  3474.       srvname   sysname not null,
  3475.       dbname    sysname not null
  3476.       )
  3477.  
  3478.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_originators'
  3479.  
  3480.       raiserror('Creating clustered index usMSrepl_originators', 0,1)
  3481.       CREATE UNIQUE CLUSTERED INDEX ucMSrepl_originators ON dbo.MSrepl_originators
  3482.          (publisher_database_id, srvname, dbname)
  3483.    END
  3484.  
  3485.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsubscriber_info' and type = 'U')
  3486.    BEGIN
  3487.       /****************************************************************************/
  3488.       raiserror('Creating table MSsubscriber_info', 0,1)
  3489.       /****************************************************************************/
  3490.       CREATE TABLE dbo.MSsubscriber_info
  3491.       (
  3492.       publisher  sysname NOT NULL,
  3493.       subscriber  sysname NOT NULL,
  3494.       type tinyint NOT NULL,           /* 0: MS SQL Server 1: ODBC Data Source */
  3495.       login sysname NULL,
  3496.       password nvarchar(524) NULL,
  3497.       description nvarchar(255) NULL,
  3498.       security_mode int NOT NULL
  3499.       )
  3500.  
  3501.       EXEC dbo.sp_MS_marksystemobject 'MSsubscriber_info'
  3502.  
  3503.       raiserror('Creating clustered index ucMSsubscriber_info', 0,1)
  3504.       CREATE UNIQUE CLUSTERED INDEX ucMSsubscriber_info ON dbo.MSsubscriber_info
  3505.      (publisher, subscriber)
  3506.    END
  3507.    ELSE
  3508.    BEGIN
  3509.       IF NOT EXISTS (select * from syscolumns
  3510.         where name = 'description'
  3511.         and id=object_id('MSsubscriber_info'))
  3512.       BEGIN
  3513.          
  3514.          ALTER TABLE MSsubscriber_info ADD description nvarchar (255) NULL
  3515.          UPDATE MSsubscriber_info SET description = 'SQL Server 6.0'
  3516.       END
  3517.    END
  3518.  
  3519.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsubscriber_schedule' and type = 'U')
  3520.    BEGIN
  3521.          raiserror('Creating table MSsubscriber_schedule', 0,1)
  3522.          
  3523.         CREATE TABLE dbo.MSsubscriber_schedule
  3524.         (
  3525.             publisher  sysname NOT NULL,
  3526.             subscriber  sysname NOT NULL,
  3527.             agent_type      smallint NOT NULL,   -- 0 for distribution agent, 1 for merge agent
  3528.             frequency_type int NOT NULL,
  3529.             frequency_interval int NOT NULL,
  3530.             frequency_relative_interval int NOT NULL,
  3531.             frequency_recurrence_factor int NOT NULL,
  3532.             frequency_subday int NOT NULL,
  3533.             frequency_subday_interval int NOT NULL,
  3534.             active_start_time_of_day int NOT NULL,
  3535.             active_end_time_of_day int NOT NULL,
  3536.             active_start_date int NOT NULL,
  3537.             active_end_date int NOT NULL
  3538.         )
  3539.       EXEC dbo.sp_MS_marksystemobject 'MSsubscriber_schedule'
  3540.  
  3541.         CREATE UNIQUE CLUSTERED INDEX ucMSsubscriber_schedule ON dbo.MSsubscriber_schedule
  3542.      (publisher, subscriber, agent_type)
  3543.      
  3544.     END
  3545.  
  3546.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsnapshot_history' and type = 'U')
  3547.    BEGIN
  3548.         /****************************************************************************/
  3549.          raiserror('Creating table MSsnapshot_history', 0,1)
  3550.         /****************************************************************************/
  3551.         CREATE TABLE dbo.MSsnapshot_history 
  3552.         (
  3553.         agent_id int NOT NULL,
  3554.         runstatus int NOT NULL,
  3555.         start_time datetime NOT NULL,        
  3556.         time datetime NOT NULL,    
  3557.         duration int NOT NULL,            
  3558.         comments nvarchar(255) NOT NULL,
  3559.  
  3560.         -- Session summary statistics
  3561.         delivered_transactions int NOT NULL,
  3562.         delivered_commands int NOT NULL,
  3563.         delivery_rate float NOT NULL,
  3564.  
  3565.         error_id int NOT NULL,
  3566.         timestamp NOT NULL
  3567.     )
  3568.  
  3569.       EXEC dbo.sp_MS_marksystemobject 'MSsnapshot_history'
  3570.  
  3571.      raiserror('Creating clustered index ucMSsnapshot_history', 0,1)
  3572.     CREATE UNIQUE CLUSTERED INDEX ucMSsnapshot_history ON dbo.MSsnapshot_history
  3573.         (agent_id, timestamp, start_time, time)
  3574.  
  3575.     if not exists (select * from sysindexes where name = 'nc1MSsnapshot_history')
  3576.     begin
  3577.          raiserror('Creating clustered index nc1MSsnapshot_history', 0,1)
  3578.         create nonclustered index nc1MSsnapshot_history on MSsnapshot_history(agent_id, start_time) 
  3579.     end    
  3580.  
  3581.    END
  3582.        
  3583.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSlogreader_history' and type = 'U')
  3584.    BEGIN
  3585.         /****************************************************************************/
  3586.         raiserror('Creating table MSlogreader_history', 0,1)
  3587.         /****************************************************************************/    
  3588.         CREATE TABLE dbo.MSlogreader_history
  3589.         (
  3590.         agent_id int NOT NULL,
  3591.         runstatus int NOT NULL,
  3592.         start_time datetime NOT NULL,
  3593.         time datetime NOT NULL,
  3594.         duration int NOT NULL,
  3595.         comments nvarchar(255) NOT NULL,
  3596.         xact_seqno varbinary(16) NULL,
  3597.  
  3598.         -- Session summary statistics
  3599.         delivery_time int NOT NULL,
  3600.         delivered_transactions int NOT NULL,
  3601.         delivered_commands int NOT NULL,
  3602.         average_commands int NOT NULL,
  3603.         delivery_rate float NOT NULL,
  3604.         delivery_latency int NOT NULL,
  3605.  
  3606.         error_id int NOT NULL,
  3607.         timestamp NOT NULL
  3608.         )
  3609.  
  3610.       EXEC dbo.sp_MS_marksystemobject 'MSlogreader_history'
  3611.  
  3612.     raiserror('Creating clustered index ucMSlogreader_history', 0,1)
  3613.     CREATE UNIQUE CLUSTERED INDEX ucMSlogreader_history ON dbo.MSlogreader_history
  3614.         (agent_id, timestamp, runstatus, start_time, time) 
  3615.  
  3616.     if not exists (select * from sysindexes where name = 'nc1MSlogreader_history')
  3617.     begin
  3618.         raiserror('Creating nonclustered index nc1MSlogreader_history', 0,1)
  3619.         create nonclustered index nc1MSlogreader_history on MSlogreader_history(agent_id, start_time) 
  3620.     end    
  3621.  
  3622.    END
  3623.  
  3624.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSdistribution_history' and type = 'U')
  3625.    BEGIN
  3626.         /****************************************************************************/
  3627.           raiserror('Creating table MSdistribution_history', 0,1)
  3628.         /****************************************************************************/    
  3629.  
  3630.         CREATE TABLE dbo.MSdistribution_history
  3631.         (
  3632.         agent_id int NOT NULL,
  3633.         runstatus int NOT NULL,
  3634.         start_time datetime NOT NULL,
  3635.         time datetime NOT NULL,
  3636.         duration int NOT NULL,
  3637.         comments nvarchar(255) NOT NULL,
  3638.         xact_seqno varbinary(16) NULL,
  3639.  
  3640.         -- Current statistics
  3641.         current_delivery_rate float NOT NULL,
  3642.         current_delivery_latency int NOT NULL,
  3643.  
  3644.         -- Session summary statistics
  3645.         delivered_transactions int NOT NULL,
  3646.         delivered_commands int NOT NULL,
  3647.         average_commands int NOT NULL,
  3648.         delivery_rate float NOT NULL,       
  3649.         delivery_latency int NOT NULL, 
  3650.  
  3651.         -- Summary statistics across all sessions
  3652.         total_delivered_commands int NOT NULL, 
  3653.  
  3654.         error_id int NOT NULL,
  3655.         updateable_row bit NOT NULL,
  3656.         timestamp NOT NULL
  3657.         )
  3658.  
  3659.     EXEC dbo.sp_MS_marksystemobject 'MSdistribution_history'
  3660.  
  3661.     raiserror('Creating clustered index ucMSdistribution_history', 0,1)
  3662.     CREATE UNIQUE CLUSTERED INDEX ucMSdistribution_history ON dbo.MSdistribution_history
  3663.         (agent_id, timestamp, runstatus, start_time, time) 
  3664.  
  3665.  
  3666.     if not exists (select * from sysindexes where name = 'nc1MSdistribution_history')
  3667.     begin
  3668.         raiserror('Creating clustered index nc1MSdistribution_history', 0,1)
  3669.         create nonclustered index nc1MSdistribution_history on MSdistribution_history(agent_id, start_time) 
  3670.     end    
  3671.  
  3672.    END
  3673.  
  3674.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSmerge_history' and type = 'U')
  3675.    BEGIN
  3676.         /****************************************************************************/
  3677.       raiserror('Creating table MSmerge_history', 0,1)
  3678.         /****************************************************************************/    
  3679.  
  3680.         CREATE TABLE dbo.MSmerge_history
  3681.         (
  3682.         agent_id                    int             NOT NULL,
  3683.         runstatus               int             NOT NULL,
  3684.         start_time              datetime        NOT NULL,
  3685.         time                    datetime        NOT NULL,
  3686.         duration                int             NOT NULL,
  3687.         comments                nvarchar(255)   NOT NULL,
  3688.         delivery_time               int         NOT NULL,
  3689.         delivery_rate               float       NOT NULL,
  3690.         publisher_insertcount       int         NULL,
  3691.         publisher_updatecount       int         NULL,
  3692.         publisher_deletecount       int         NULL,
  3693.         publisher_conflictcount     int         NULL,
  3694.         subscriber_insertcount      int         NULL,
  3695.         subscriber_updatecount      int         NULL,
  3696.         subscriber_deletecount      int         NULL,
  3697.         subscriber_conflictcount    int         NULL,
  3698.         error_id                    int         NOT NULL,
  3699.         timestamp                               NOT NULL,
  3700.         updateable_row                bit            NOT NULL
  3701.         )        
  3702.  
  3703.     EXEC dbo.sp_MS_marksystemobject 'MSmerge_history'
  3704.  
  3705.     raiserror('Creating clustered index ucMSmerge_history', 0,1)
  3706.     CREATE UNIQUE CLUSTERED INDEX ucMSmerge_history ON dbo.MSmerge_history
  3707.         (agent_id, timestamp, runstatus) 
  3708.         
  3709.     if not exists (select * from sysindexes where name = 'nc1MSmerge_history')
  3710.         begin
  3711.             raiserror('Creating nonclustered index nc1MSmerge_history', 0,1)
  3712.             create nonclustered index nc1MSmerge_history on MSmerge_history(agent_id, runstatus, start_time) 
  3713.         end
  3714.    END
  3715.    ELSE
  3716.    BEGIN
  3717.       IF NOT EXISTS (select * from syscolumns
  3718.         where name = 'updateable_row'
  3719.         and id=object_id('MSmerge_history'))
  3720.       BEGIN 
  3721.          ALTER TABLE MSmerge_history ADD updateable_row bit default 1 NOT NULL
  3722.       END
  3723.  
  3724.       if not exists (select * from sysindexes where name = 'nc1MSmerge_history' and id=object_id('MSmerge_history'))
  3725.       begin
  3726.             raiserror('Creating nonclustered index nc1MSmerge_history', 0,1)
  3727.             create nonclustered index nc1MSmerge_history on MSmerge_history(agent_id, runstatus, start_time) 
  3728.       end    
  3729.    END
  3730.  
  3731.  
  3732.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_errors' and type = 'U')
  3733.    BEGIN
  3734.         /****************************************************************************/
  3735.         raiserror('Creating table MSrepl_errors', 0,1)
  3736.         /****************************************************************************/    
  3737.  
  3738.         CREATE TABLE dbo.MSrepl_errors
  3739.         (
  3740.         id                  int NOT NULL,
  3741.         time                datetime NOT NULL,
  3742.         error_type_id       int NULL,
  3743.         source_type_id      int NULL,
  3744.         source_name         nvarchar(100) NULL,
  3745.         error_code          sysname NULL,
  3746.         error_text          ntext NULL,
  3747.         xact_seqno            varbinary(16) NULL,
  3748.         command_id            int NULL
  3749.         )
  3750.  
  3751.       EXEC dbo.sp_MS_marksystemobject 'MSrepl_errors'
  3752.  
  3753.     raiserror('Creating clustered index ucMSrepl_errors', 0,1)
  3754.     CREATE CLUSTERED INDEX ucMSrepl_errors ON dbo.MSrepl_errors
  3755.         (id, time) -- WITH ALLOW_DUP_ROW
  3756.    END
  3757.    ELSE
  3758.    BEGIN
  3759.       IF NOT EXISTS (select * from syscolumns
  3760.         where name = 'xact_seqno'
  3761.         and id=object_id('MSrepl_errors'))
  3762.          ALTER TABLE MSrepl_errors ADD xact_seqno varbinary(16) NULL
  3763.  
  3764.       IF NOT EXISTS (select * from syscolumns
  3765.         where name = 'command_id'
  3766.         and id=object_id('MSrepl_errors'))
  3767.          ALTER TABLE MSrepl_errors ADD command_id int NULL
  3768.    END
  3769.  
  3770.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSsnapshot_agents' and type = 'U')
  3771.    BEGIN
  3772.         /****************************************************************************/
  3773.         raiserror('Creating table MSsnapshot_agents', 0,1)
  3774.         /****************************************************************************/    
  3775.  
  3776.         CREATE TABLE dbo.MSsnapshot_agents
  3777.         (
  3778.         id                  int IDENTITY NOT NULL,
  3779.         name                nvarchar(100) NOT NULL,
  3780.         publisher_id        smallint NOT NULL,
  3781.         publisher_db        sysname NOT NULL,
  3782.         publication         sysname NOT NULL,
  3783.         publication_type    int NOT NULL,       -- 0 transactional 1 snapshot 2 merge
  3784.         local_job           bit NOT NULL,
  3785.         job_id              binary(16) NULL,
  3786.         profile_id  int     NOT NULL
  3787.         )
  3788.  
  3789.       EXEC dbo.sp_MS_marksystemobject 'MSsnapshot_agents'
  3790.  
  3791.     raiserror('Creating clustered index ucMSsnapshot_agents', 0,1)
  3792.     CREATE CLUSTERED INDEX ucMSsnapshot_agents ON dbo.MSsnapshot_agents
  3793.         (publication, publisher_db, publisher_id) 
  3794.  
  3795.     raiserror('Creatingindex iMSsnapshot_agents', 0,1)
  3796.     CREATE UNIQUE INDEX iMSsnapshot_agents ON dbo.MSsnapshot_agents
  3797.         (id)
  3798.    END
  3799.  
  3800.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSlogreader_agents' and type = 'U')
  3801.    BEGIN
  3802.         /****************************************************************************/
  3803.         raiserror('Creating table MSlogreader_agents', 0,1)
  3804.         /****************************************************************************/    
  3805.  
  3806.         CREATE TABLE dbo.MSlogreader_agents
  3807.         (
  3808.         id                  int IDENTITY NOT NULL,
  3809.         name                nvarchar(100) NOT NULL,
  3810.         publisher_id        smallint NOT NULL,
  3811.         publisher_db        sysname NOT NULL,
  3812.         publication         sysname NOT NULL,-- Not used for SQL Server publisher
  3813.         local_job           bit NOT NULL,
  3814.         job_id              binary(16) NULL,
  3815.         profile_id  int     NOT NULL
  3816.         )
  3817.  
  3818.       EXEC dbo.sp_MS_marksystemobject 'MSlogreader_agents'
  3819.  
  3820.     raiserror('Creating clustered index ucMSlogreader_agents', 0,1)
  3821.     CREATE CLUSTERED INDEX ucMSlogreader_agents ON dbo.MSlogreader_agents
  3822.         (publisher_db, publisher_id) 
  3823.  
  3824.     raiserror('Creatingindex iMSlogreader_agents', 0,1)
  3825.     CREATE UNIQUE INDEX iMSlogreader_agents ON dbo.MSlogreader_agents
  3826.         (id)
  3827.    END
  3828.  
  3829.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSdistribution_agents' and type = 'U')
  3830.    BEGIN
  3831.         /****************************************************************************/
  3832.         raiserror('Creating table MSdistribution_agents', 0,1)
  3833.         /****************************************************************************/    
  3834.  
  3835.         CREATE TABLE dbo.MSdistribution_agents
  3836.         (
  3837.         id                  int IDENTITY NOT NULL,
  3838.         name                nvarchar(100) NOT NULL,
  3839.         publisher_database_id int NOT NULL,
  3840.         publisher_id        smallint NOT NULL,
  3841.         publisher_db        sysname NOT NULL,
  3842.         publication         sysname NOT NULL,
  3843.         subscriber_id       smallint NULL, 
  3844.         subscriber_db       sysname NULL,
  3845.         subscription_type   int NOT NULL,
  3846.         local_job           bit NULL,
  3847.         job_id              binary(16) NULL,
  3848.         subscription_guid   binary(16) NOT NULL,
  3849.         profile_id  int     NOT NULL,
  3850.         anonymous_subid     uniqueidentifier NULL,
  3851.         subscriber_name     sysname NULL,
  3852.         virtual_agent_id    int NULL,
  3853.         anonymous_agent_id  int NULL,
  3854.         creation_date        datetime default (getdate()) not NULL,
  3855.         queue_id            sysname null,
  3856.         queue_status        int default 0 not null,
  3857.         offload_enabled     bit default 0 NOT NULL,
  3858.         offload_server      sysname NULL,
  3859.         dts_package_name    sysname NULL,
  3860.         dts_package_password nvarchar(524) NULL,
  3861.         dts_package_location int default 0 not null,
  3862.         sid                    varbinary(85) default suser_sid() not null,
  3863.         queue_server        sysname NULL
  3864.         )
  3865.  
  3866.         EXEC dbo.sp_MS_marksystemobject 'MSdistribution_agents'
  3867.  
  3868.         raiserror('Creating clustered index ucMSdistribution_agents', 0,1)
  3869.         CREATE UNIQUE CLUSTERED INDEX ucMSdistribution_agents ON dbo.MSdistribution_agents
  3870.             (id)
  3871.  
  3872.         raiserror('Creatingindex iMSdistribution_agents', 0,1)
  3873.         CREATE INDEX iMSdistribution_agents ON dbo.MSdistribution_agents
  3874.             (publication, publisher_db, publisher_id, subscriber_id, subscriber_db, anonymous_subid) 
  3875.    END
  3876.  
  3877.     if not exists (select * from syscolumns where 
  3878.         id = object_id('MSdistribution_agents') and
  3879.         name = 'creation_date')
  3880.     begin
  3881.         alter table MSdistribution_agents add creation_date datetime default (getdate()) not null
  3882.     end 
  3883.  
  3884.     if not exists (select * from syscolumns where 
  3885.         id = object_id('MSdistribution_agents') and
  3886.         name = 'offload_enabled')
  3887.     begin
  3888.         alter table MSdistribution_agents add offload_enabled bit default 0 not null
  3889.     end 
  3890.  
  3891.     if not exists (select * from syscolumns where 
  3892.         id = object_id('MSdistribution_agents') and
  3893.         name = 'offload_server')
  3894.     begin
  3895.         alter table MSdistribution_agents add offload_server sysname null
  3896.     end 
  3897.  
  3898.     if not exists (select * from syscolumns where 
  3899.         id = object_id('MSdistribution_agents') and
  3900.         name = 'queue_id')
  3901.     begin
  3902.         alter table MSdistribution_agents add queue_id sysname null
  3903.     end 
  3904.  
  3905.     if not exists (select * from syscolumns where 
  3906.         id = object_id('MSdistribution_agents') and
  3907.         name = 'queue_status')
  3908.     begin
  3909.         alter table MSdistribution_agents add queue_status int default 0 not null
  3910.     end 
  3911.  
  3912.     if not exists (select * from syscolumns where 
  3913.         id = object_id('MSdistribution_agents') and
  3914.         name = 'dts_package_name')
  3915.     begin
  3916.         alter table MSdistribution_agents add dts_package_name sysname null
  3917.     end 
  3918.     if not exists (select * from syscolumns where 
  3919.         id = object_id('MSdistribution_agents') and
  3920.         name = 'dts_package_password')
  3921.     begin
  3922.         alter table MSdistribution_agents add dts_package_password nvarchar(524) null
  3923.     end 
  3924.  
  3925.     if not exists (select * from syscolumns where 
  3926.         id = object_id('MSdistribution_agents') and
  3927.         name = 'dts_package_location')
  3928.     begin
  3929.         alter table MSdistribution_agents add dts_package_location int default 0 not null
  3930.     end 
  3931.  
  3932.     if not exists (select * from syscolumns where 
  3933.         id = object_id('MSdistribution_agents') and
  3934.         name = 'sid')
  3935.     begin
  3936.         -- set sid to be the upgrade user. db_owner or sysadmin
  3937.         -- can drop the agent entry
  3938.         alter table MSdistribution_agents add sid varbinary(85) default suser_sid() not null
  3939.     end 
  3940.  
  3941.     if not exists (select * from syscolumns where 
  3942.         id = object_id('MSdistribution_agents') and
  3943.         name = 'queue_server')
  3944.     begin
  3945.         alter table MSdistribution_agents add queue_server sysname null
  3946.         EXEC dbo.sp_MSupdate_mqserver_distdb
  3947.     end 
  3948.     
  3949.    IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSmerge_agents' and type = 'U')
  3950.    BEGIN
  3951.         /****************************************************************************/
  3952.         raiserror('Creating table MSmerge_agents', 0,1)
  3953.         /****************************************************************************/    
  3954.  
  3955.         CREATE TABLE dbo.MSmerge_agents
  3956.         (
  3957.         id                  int IDENTITY NOT NULL,
  3958.         name                nvarchar(100) NOT NULL,
  3959.         publisher_id        smallint NOT NULL,
  3960.         publisher_db        sysname NOT NULL,
  3961.         publication         sysname NOT NULL,
  3962.         subscriber_id       smallint NULL,
  3963.         subscriber_db       sysname NULL,
  3964.         local_job           bit NULL,
  3965.         job_id              binary(16) NULL,
  3966.         profile_id          int NULL,
  3967.         anonymous_subid     uniqueidentifier NULL,
  3968.         subscriber_name     sysname NULL,
  3969.         creation_date        datetime default (getdate()) not NULL,
  3970.         offload_enabled     bit default 0 NOT NULL,
  3971.         offload_server      sysname NULL,
  3972.         sid                    varbinary(85) default suser_sid() not null
  3973.         )
  3974.  
  3975.       EXEC dbo.sp_MS_marksystemobject 'MSmerge_agents'
  3976.  
  3977.     raiserror('Creating clustered index ucMSmerge_agents', 0,1)
  3978.     CREATE CLUSTERED INDEX ucMSmerge_agents ON dbo.MSmerge_agents
  3979.         (publication, publisher_db, publisher_id, subscriber_id, subscriber_db, anonymous_subid) 
  3980.     
  3981.     raiserror('Creatingindex iMSmerge_agents', 0,1)
  3982.     CREATE UNIQUE INDEX iMSmerge_agents ON dbo.MSmerge_agents
  3983.         (id)
  3984.     END
  3985.  
  3986.  
  3987.     -- Need to re_visit the indexing of this table
  3988.     if not exists (select * from sysobjects where name = 'MSrepl_identity_range')
  3989.         begin        
  3990.             raiserror('Creating table MSrepl_identity_range',0,1)
  3991.             
  3992.             create table dbo.MSrepl_identity_range (
  3993.             publisher                sysname not NULL, 
  3994.             publisher_db            sysname    not NULL,
  3995.             tablename                sysname not NULL,
  3996.             identity_support        int NULL,
  3997.             next_seed                bigint NULL, --resource control
  3998.             pub_range                bigint NULL, --publisher range
  3999.             range                    bigint NULL, -- set by sp_addmergearticle
  4000.             max_identity            bigint NULL, --resource control
  4001.             threshold                int    NULL,    --in percentage, set by sp_addmergearticle
  4002.             current_max                bigint NULL    --max value for current check constraint,set by sp_addmergearticle
  4003.             )
  4004.             exec dbo.sp_MS_marksystemobject MSrepl_identity_range
  4005.  
  4006.             grant select on MSrepl_identity_range to public
  4007.         end
  4008.  
  4009.     if not exists (select * from syscolumns where 
  4010.         id = object_id('MSmerge_agents') and
  4011.         name = 'creation_date')
  4012.     begin
  4013.         alter table MSmerge_agents add creation_date datetime default (getdate()) not null
  4014.     end 
  4015.  
  4016.     if not exists (select * from syscolumns where 
  4017.         id = object_id('MSmerge_agents') and
  4018.         name = 'offload_enabled')
  4019.     begin
  4020.         alter table MSmerge_agents add offload_enabled bit default 0 not null
  4021.     end 
  4022.  
  4023.     if not exists (select * from syscolumns where 
  4024.         id = object_id('MSmerge_agents') and
  4025.         name = 'offload_server')
  4026.     begin
  4027.         alter table MSmerge_agents add offload_server sysname null
  4028.     end 
  4029.  
  4030.     if not exists (select * from syscolumns where 
  4031.         id = object_id('MSmerge_agents') and
  4032.         name = 'sid')
  4033.     begin
  4034.         -- set sid to be the upgrade user. db_owner or sysadmin
  4035.         -- can drop the agent entry
  4036.         alter table MSmerge_agents add sid varbinary(85) default suser_sid() not null
  4037.     end 
  4038.  
  4039.     IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSpublication_access' and type = 'U')
  4040.     BEGIN
  4041.         /****************************************************************************/
  4042.         raiserror('Creating table MSpublication_access', 0,1)
  4043.         /****************************************************************************/    
  4044.  
  4045.         CREATE TABLE dbo.MSpublication_access
  4046.         (
  4047.         publication_id      int NULL,       -- Publication_id is unique in distribution database.
  4048.         login               sysname NOT NULL
  4049.                                             -- Logins in the publication access list, they nust
  4050.                                             -- exist at both publisher and distributor side.            
  4051.         )
  4052.  
  4053.         EXEC dbo.sp_MS_marksystemobject 'MSpublication_access'
  4054.  
  4055.         raiserror('Creating clustered index ucMSpublication_access', 0,1)
  4056.         CREATE UNIQUE CLUSTERED INDEX ucMSpublication_access ON dbo.MSpublication_access
  4057.             (publication_id, login) 
  4058.     END
  4059.  
  4060.     -- For b3 upgrade
  4061.     if NOT EXISTS (select * from syscolumns where name='retention' and id=object_id('MSpublications'))
  4062.     begin
  4063.         alter table MSpublications add retention int NULL 
  4064.         UPDATE msdb..MSdistributiondbs set max_distretention=72 where
  4065.             name = db_name() collate database_default
  4066.     end
  4067.  
  4068.     -- drop default_access column
  4069.     if exists (select * from syscolumns where id = object_id('MSpublications') and
  4070.         name = 'default_access')
  4071.     begin
  4072.         alter table MSpublications drop column default_access
  4073.     end 
  4074.  
  4075.     -- Drop publisher_id column
  4076.     if EXISTS (select * from syscolumns where name='publisher_id' and id=object_id('MSpublication_access'))
  4077.     begin
  4078.         drop index MSpublication_access.ucMSpublication_access
  4079.  
  4080.         alter table MSpublication_access drop column publisher_id
  4081.  
  4082.         raiserror('Creating clustered index ucMSpublication_access', 0,1)
  4083.         CREATE CLUSTERED INDEX ucMSpublication_access ON dbo.MSpublication_access
  4084.         (publication_id, login) 
  4085.     end
  4086.  
  4087.     IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSqreader_agents' and type = 'U')
  4088.     BEGIN
  4089.         /****************************************************************************/
  4090.         raiserror('Creating table MSqreader_agents', 0,1)
  4091.         /****************************************************************************/    
  4092.  
  4093.         CREATE TABLE dbo.MSqreader_agents
  4094.         (
  4095.         id                  int IDENTITY NOT NULL,
  4096.         name                nvarchar(100) NULL,
  4097.         job_id              binary(16) NULL,
  4098.         profile_id          int NULL
  4099.         )
  4100.  
  4101.       EXEC dbo.sp_MS_marksystemobject 'MSqreader_agents'
  4102.  
  4103.     raiserror('Creating unique index ucMSqreader_agents', 0,1)
  4104.     CREATE UNIQUE INDEX ucMSqreader_agents ON dbo.MSqreader_agents
  4105.         (id)
  4106.    END
  4107.  
  4108.     -- add columns for existing table
  4109.     if not exists (select * from syscolumns where 
  4110.         id = object_id('MSqreader_agents') and
  4111.         name = 'profile_id')
  4112.     begin
  4113.         alter table dbo.MSqreader_agents add profile_id int NULL
  4114.     end 
  4115.    
  4116.  
  4117.     IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSqreader_history' and type = 'U')
  4118.     BEGIN
  4119.     /****************************************************************************/
  4120.     raiserror('Creating table MSqreader_history', 0,1)
  4121.     /****************************************************************************/    
  4122.  
  4123.     CREATE TABLE dbo.MSqreader_history
  4124.     (
  4125.     agent_id int NOT NULL,
  4126.     publication_id int NULL,
  4127.     runstatus int NOT NULL,
  4128.     start_time datetime NOT NULL,
  4129.     time datetime NOT NULL,
  4130.     duration int NOT NULL,
  4131.     comments nvarchar(255) NOT NULL,
  4132.     transaction_id nvarchar(40) NULL, 
  4133.     transaction_status int NULL,
  4134.     transactions_processed int NULL DEFAULT 0,
  4135.     commands_processed int NULL DEFAULT 0,
  4136.     delivery_rate float NOT NULL DEFAULT 0.0,
  4137.     transaction_rate float NOT NULL DEFAULT 0.0,
  4138.     subscriber sysname NULL,
  4139.     subscriberdb sysname NULL,
  4140.     error_id int NULL,    
  4141.     timestamp NOT NULL
  4142.     )
  4143.  
  4144.     EXEC dbo.sp_MS_marksystemobject 'MSqreader_history'
  4145.  
  4146.     raiserror('Creating clustered index ucMSqreader_history', 0,1)
  4147.     CREATE CLUSTERED INDEX ucMSqreader_history ON dbo.MSqreader_history
  4148.     (agent_id, timestamp, runstatus, start_time, time) 
  4149.     END
  4150.  
  4151.     -- alter column publication_id
  4152.     if not exists (select * from syscolumns where 
  4153.         id = object_id('MSqreader_history') and
  4154.         name = 'publication_id')
  4155.     begin
  4156.         alter table dbo.MSqreader_history alter column publication_id int NULL
  4157.     end
  4158.  
  4159.     -- add columns for existing table
  4160.     if not exists (select * from syscolumns where 
  4161.         id = object_id('MSqreader_history') and
  4162.         name = 'error_id')
  4163.     begin
  4164.         alter table dbo.MSqreader_history add error_id int NULL
  4165.     end 
  4166.  
  4167.     if not exists (select * from syscolumns where 
  4168.         id = object_id('MSqreader_history') and
  4169.         name = 'transactions_processed')
  4170.     begin
  4171.         alter table dbo.MSqreader_history add transactions_processed int NULL DEFAULT 0
  4172.     end 
  4173.  
  4174.     if not exists (select * from syscolumns where 
  4175.         id = object_id('MSqreader_history') and
  4176.         name = 'delivery_rate')
  4177.     begin
  4178.         alter table dbo.MSqreader_history add delivery_rate float NOT NULL DEFAULT 0.0
  4179.     end 
  4180.  
  4181.     if not exists (select * from syscolumns where 
  4182.         id = object_id('MSqreader_history') and
  4183.         name = 'transaction_rate')
  4184.     begin
  4185.         alter table dbo.MSqreader_history add transaction_rate float NOT NULL DEFAULT 0.0
  4186.     end 
  4187.  
  4188.     IF NOT EXISTS (SELECT * FROM sysobjects WHERE name = 'MSrepl_backup_lsns' and type = 'U')
  4189.     BEGIN
  4190.         /****************************************************************************/
  4191.         raiserror('Creating table MSrepl_backup_lsns', 0,1)
  4192.         /****************************************************************************/    
  4193.  
  4194.         CREATE TABLE dbo.MSrepl_backup_lsns
  4195.         (
  4196.         publisher_database_id int NOT NULL,
  4197.         valid_xact_id varbinary(16) NULL,
  4198.         valid_xact_seqno varbinary (16 ) NULL,
  4199.         next_xact_id varbinary(16) NULL,
  4200.         next_xact_seqno varbinary (16 )  NULL
  4201.         )
  4202.  
  4203.         EXEC dbo.sp_MS_marksystemobject 'MSrepl_backup_lsns'
  4204.  
  4205.         raiserror('Creating clustered index ucMSrepl_backup_lsns', 0,1)
  4206.         CREATE UNIQUE CLUSTERED INDEX ucMSrepl_backup_lsns ON dbo.MSrepl_backup_lsns
  4207.             (publisher_database_id)
  4208.  
  4209.         if exists (select * from MSpublisher_databases)
  4210.         begin
  4211.             /****************************************************************************/
  4212.             raiserror('Upgrading MSrepl_backup_lsns', 0,1)
  4213.             /****************************************************************************/    
  4214.             insert into MSrepl_backup_lsns select d.id, 
  4215.                 NULL, NULL, NULL, NULL from MSpublisher_databases d
  4216.             -- No need to set the lsns in the table since the 'sync with backup' option is 
  4217.             -- new in 8.0
  4218.         end
  4219.  
  4220.     END
  4221. GO
  4222.  
  4223. dump tran master with no_log
  4224. go
  4225.  
  4226. --
  4227. -- sp_MSupdate_mqserver_distdb
  4228. --
  4229. -- This procedure is called to upgrade the MSMQ related entries in 
  4230. -- MSdistribution_agents in distribution database - this processing 
  4231. -- cannot be done directly inside sp_MScreate_dist_tables or sp_vupgrade_distdb
  4232. -- since we need the queue_server column for the processing and we can
  4233. -- get a syntax error if the table exists without the column
  4234. --
  4235. -- Assumption : MSsubscription_agents has been created 
  4236. --                and column queue_server exists
  4237. --
  4238. raiserror('Creating procedure sp_MSupdate_mqserver_distdb', 0,1)
  4239. go
  4240. create procedure sp_MSupdate_mqserver_distdb
  4241. as
  4242. begin
  4243.     if exists (select * from MSdistribution_agents
  4244.         where queue_id IS NOT NULL and queue_id != N'mssqlqueue' and queue_server IS NULL)
  4245.     begin
  4246.         --
  4247.         -- we have entries for active Queued subscriptions using MSMQ that need upgrade
  4248.         --
  4249.         declare @queue_server sysname
  4250.  
  4251.         --
  4252.         -- prepare the queue server name
  4253.         --
  4254.         select @queue_server = @@servername
  4255.         if (charindex(N'\', @queue_server) > 0)
  4256.             select @queue_server = substring(@queue_server, 1, charindex(N'\', @queue_server) - 1)
  4257.  
  4258.         --
  4259.         -- update the queue_server column for these entries
  4260.         --
  4261.         update MSdistribution_agents
  4262.         set queue_server = @queue_server
  4263.         where queue_id IS NOT NULL and queue_id != N'mssqlqueue' and queue_server IS NULL
  4264.     end
  4265. end
  4266. go
  4267.  
  4268. raiserror('Creating procedure sp_MSadd_distributor_alerts_and_responses', 0,1)
  4269. go
  4270. create procedure sp_MSadd_distributor_alerts_and_responses
  4271.     @from_scripting bit = 0
  4272. as
  4273.  
  4274.     declare @description    nvarchar(500)
  4275.     declare @category_name  sysname
  4276.     declare @agent_name     sysname
  4277.     declare @response_job   nvarchar(100)
  4278.     declare @alert_name     nvarchar(100)
  4279.     declare @alert_id       int
  4280.     declare @retcode        int
  4281.  
  4282.     --
  4283.     -- Add replication alert response jobs
  4284.     --
  4285.     select @category_name = name FROM msdb.dbo.syscategories where category_id = 18
  4286.  
  4287.     -- Add Validation failure response job
  4288.     -- (20570,10,0,'Reinitialize Subscriptions On Validation Failure.', 1033)   -- Title of a alert response job
  4289.     -- (20571,10,0,'Reiniitializes all subscriptions that have failed due to data validation failures.', 1033)  -- Description of a alert response job
  4290.     set @response_job = formatmessage(20570)
  4291.     set @description = formatmessage(20571)
  4292.     
  4293.     if @from_scripting = 0
  4294.     begin
  4295.         -- Delete the job if it already exists
  4296.         if (exists (select * from msdb..sysjobs_view where name = @response_job collate database_default))
  4297.         begin
  4298.             exec @retcode = msdb.dbo.sp_delete_job @job_name = @response_job
  4299.             if @@ERROR <> 0 or @retcode <> 0
  4300.                 return (1)
  4301.         end
  4302.  
  4303.         execute @retcode = msdb.dbo.sp_MSadd_repl_job 
  4304.             @response_job,
  4305.             @subsystem = 'TSQL',
  4306.             @server = @@SERVERNAME,
  4307.             @databasename = 'master',
  4308.             @description = @description,
  4309.             @command = 'exec dbo.sp_MSreinit_failed_subscriptions @failure_level = 1',
  4310.             @enabled = 1,
  4311.             @loghistcompletionlevel = 0,
  4312.             @retryattempts = 0,
  4313.             @category_name = @category_name
  4314.         if @@ERROR <> 0 or @retcode <> 0
  4315.             return (1)
  4316.     end
  4317.  
  4318.     --
  4319.     -- Add replication alerts
  4320.     --
  4321.  
  4322.     -- Get Replication category name (assumes category_id = 20)
  4323.     select @category_name = name FROM msdb.dbo.syscategories where category_id = 20
  4324.  
  4325.     -- Add Failure Alert
  4326.     set @alert_name = formatmessage(20536)  
  4327.     set @alert_id = 14151 -- corresponding to formatmessage(20536),  Failure alert
  4328.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4329.     begin
  4330.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 14151
  4331.         if @@error <> 0 or @retcode <> 0
  4332.             return 1
  4333.     end
  4334.  
  4335.     -- Add Retry Alert
  4336.     set @alert_name=formatmessage(20537)
  4337.     set @alert_id = 14152 -- corresponding to formatmessage(20537),  Retry alert
  4338.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4339.     begin
  4340.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 14152
  4341.         if @@error <> 0 or @retcode <> 0
  4342.             return 1
  4343.     end
  4344.  
  4345.     -- Add Success Alert
  4346.     set @alert_name=formatmessage(20540)
  4347.     set @alert_id = 14150 -- corresponding to formatmessage(20540), 
  4348.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4349.     begin
  4350.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 14150
  4351.         if @@error <> 0 or @retcode <> 0
  4352.             return 1
  4353.     end
  4354.  
  4355.     -- Add Shutdown request Alert
  4356.     set @alert_name=formatmessage(20578)
  4357.     set @alert_id = 20578 --  Custom agent shutdown message
  4358.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4359.     begin
  4360.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 20578
  4361.         if @@error <> 0 or @retcode <> 0
  4362.             return 1
  4363.     end
  4364.  
  4365.     -- Add Validation Failure Alert
  4366.     set @alert_name=formatmessage(20565)
  4367.     set @alert_id = 20574 -- corresponding to formatmessage(20574), 
  4368.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4369.     begin
  4370.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 20574
  4371.         if @@error <> 0 or @retcode <> 0
  4372.             return 1
  4373.     end
  4374.  
  4375.     -- Add Validation Success Alert
  4376.     set @alert_name=formatmessage(20566)
  4377.     set @alert_id = 20575 -- corresponding to formatmessage(20575), 
  4378.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4379.     begin
  4380.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 20575
  4381.         if @@error <> 0 or @retcode <> 0
  4382.             return 1
  4383.     end
  4384.  
  4385.     -- Add Reinitialized after Validation Failure
  4386.     set @alert_name=formatmessage(20573)
  4387.     set @alert_id = 20572 -- corresponding to formatmessage(20566),  
  4388.     if not exists (select * from msdb.dbo.sysalerts where message_id = @alert_id)
  4389.     begin
  4390.         exec @retcode = msdb.dbo.sp_add_alert @enabled = 0, @name = @alert_name, @category_name = @category_name, @message_id = 20572
  4391.         if @@error <> 0 or @retcode <> 0
  4392.             return 1
  4393.     end
  4394.  
  4395. GO
  4396.  
  4397.  
  4398. raiserror('Creating procedure sp_MSdrop_distributor_alerts_and_responses', 0,1)
  4399. go
  4400. create proc sp_MSdrop_distributor_alerts_and_responses
  4401. as
  4402.  
  4403.     declare @name           nvarchar(100)
  4404.     declare @alert_id       int
  4405.     declare @retcode        int
  4406.  
  4407.     --
  4408.     -- Delete alerts and response jobs
  4409.     --
  4410.  
  4411.     -- Drop Replication Checkup Agent
  4412.     select @name = formatmessage(20533)
  4413.     IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @name collate database_default and
  4414.         UPPER(originating_server) = UPPER(CONVERT(sysname, SERVERPROPERTY('ServerName'))))
  4415.     BEGIN
  4416.         EXEC @retcode = msdb.dbo.sp_delete_job  @job_name = @name
  4417.         IF @@ERROR <> 0 or @retcode <> 0
  4418.         BEGIN
  4419.             return (1)            
  4420.         END
  4421.     END
  4422.  
  4423.     -- Drop Reinit subscription response job
  4424.     set @name = formatmessage(20570)
  4425.     IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @name collate database_default and
  4426.         UPPER(originating_server) = UPPER(CONVERT(sysname, SERVERPROPERTY('ServerName'))))
  4427.     BEGIN
  4428.         EXEC @retcode = msdb.dbo.sp_delete_job  @job_name = @name
  4429.         IF @@ERROR <> 0 or @retcode <> 0
  4430.         BEGIN
  4431.             return (1)            
  4432.         END
  4433.     END
  4434.  
  4435.     -- Drop the alerts
  4436.     set @alert_id = 14150 -- success alert
  4437.     set @name=formatmessage(20540)
  4438.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4439.     begin
  4440.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4441.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4442.         if @@error <> 0 or @retcode <> 0
  4443.             return (1)            
  4444.     end
  4445.     
  4446.     set @alert_id = 14151 -- failure alert
  4447.     set @name = formatmessage(20536)
  4448.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4449.     begin
  4450.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4451.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4452.         if @@error <> 0 or @retcode <> 0
  4453.             return (1)            
  4454.     end
  4455.     
  4456.     set @alert_id = 14152 -- retry alert
  4457.     set @name = formatmessage(20537)
  4458.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4459.     begin
  4460.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4461.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4462.         if @@error <> 0 or @retcode <> 0
  4463.             return (1)            
  4464.     end
  4465.  
  4466.     set @alert_id = 14153 -- warnning alert
  4467.     set @name = formatmessage(20540)
  4468.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4469.     begin
  4470.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4471.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4472.         if @@error <> 0 or @retcode <> 0
  4473.             return (1)            
  4474.     end
  4475.  
  4476.     -- Remove Validation Failure Alert
  4477.     set @alert_id = 20574 
  4478.     set @name = formatmessage(20565)
  4479.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4480.     begin
  4481.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4482.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4483.         if @@error <> 0 or @retcode <> 0
  4484.             return (1)            
  4485.     end
  4486.  
  4487.     -- Remove Validation Sucess Alert
  4488.     set @alert_id = 20575
  4489.     set @name = formatmessage(20566)
  4490.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4491.     begin
  4492.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4493.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4494.         if @@error <> 0 or @retcode <> 0
  4495.             return (1)            
  4496.     end
  4497.     
  4498.     -- Remove Reinitialized after Validation Failure
  4499.     set @alert_id = 20525 -- checksum alert
  4500.     set @name = formatmessage(20573)
  4501.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4502.     begin
  4503.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4504.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4505.         if @@error <> 0 or @retcode <> 0
  4506.             return (1)            
  4507.     end
  4508.  
  4509.     -- Remove subscription reinitialized after validation failure
  4510.     set @alert_id = 20572 -- corresponding to formatmessage(20566),  
  4511.     set @name=formatmessage(20573)
  4512.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4513.     begin
  4514.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4515.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4516.         if @@error <> 0 or @retcode <> 0
  4517.             return (1)            
  4518.     end
  4519.  
  4520.     -- Remove Shutdown request Alert
  4521.     set @alert_id = 20578 --  Custom agent shutdown message
  4522.     set @name=formatmessage(20578)
  4523.     if exists (select * from msdb.dbo.sysalerts where message_id=@alert_id)
  4524.     begin
  4525.         select @name=name from msdb.dbo.sysalerts where message_id=@alert_id
  4526.         exec @retcode = msdb.dbo.sp_delete_alert @name
  4527.         if @@error <> 0 or @retcode <> 0
  4528.             return (1)            
  4529.     end
  4530.  
  4531. GO
  4532. raiserror('Creating procedure sp_adddistributor', 0,1)
  4533. go
  4534.  
  4535. CREATE PROCEDURE sp_adddistributor (
  4536.     @distributor sysname,            /* distributor server name */
  4537.     @heartbeat_interval int = 10,    -- minutes
  4538.     @password sysname = NULL,
  4539.     @from_scripting bit = 0
  4540.  
  4541. ) AS
  4542.  
  4543.     SET NOCOUNT ON
  4544.  
  4545.     /*
  4546.     ** Declarations.
  4547.     */
  4548.     DECLARE @retcode int
  4549.     DECLARE @agentname nvarchar(100)
  4550.     DECLARE @command nvarchar(255)
  4551.     DECLARE @distribution_db sysname
  4552.     DECLARE @distproc nvarchar(255)
  4553.     DECLARE @dist_rpcname sysname
  4554.     DECLARE @server_added bit
  4555.     declare @login sysname
  4556.  
  4557.     select @server_added = 0 
  4558.     select @login = 'distributor_admin'
  4559.  
  4560.     /* 
  4561.     ** Check if replication components are installed on this server
  4562.     */
  4563.     exec @retcode = dbo.sp_MS_replication_installed
  4564.     if (@retcode <> 1)
  4565.     begin
  4566.         return (1)
  4567.     end
  4568.  
  4569.     -- Must be at master db.
  4570.     IF db_name() <> 'master'
  4571.     BEGIN
  4572.         RAISERROR(5001, 16,-1)
  4573.         return (1)
  4574.     END
  4575.     /*
  4576.     ** Parameter Check:  @distributor.
  4577.     ** Check to make sure that the distributor is not NULL and that it
  4578.     ** conforms to the rules for identifiers.
  4579.     */
  4580.     IF @distributor IS NULL
  4581.         BEGIN
  4582.             RAISERROR (14043, 16, -1, '@distributor')
  4583.             RETURN (1)
  4584.         END
  4585.  
  4586.     EXECUTE @retcode = dbo.sp_validname @distributor
  4587.  
  4588.     IF @@ERROR <> 0 OR @retcode <> 0
  4589.        RETURN (1)
  4590.  
  4591.     IF @password = N''
  4592.         select @password = NULL
  4593.     
  4594.     -- Seed default password with random value only for a local distributor.
  4595.     IF (@password is null) AND (UPPER(@@SERVERNAME) = UPPER(@distributor)) 
  4596.         select @password = convert(sysname, newid())
  4597.     /*
  4598.     ** Check to make sure that the distributor doesn't already exist.
  4599.     */
  4600.     SELECT @dist_rpcname = NULL
  4601.     SELECT @dist_rpcname = srvname FROM master..sysservers
  4602.               WHERE  srvstatus & 8 <> 0
  4603.     IF @dist_rpcname IS NOT NULL
  4604.     BEGIN
  4605.         RAISERROR (14099, 16, -1, @dist_rpcname)
  4606.         RETURN(1)
  4607.     END
  4608.  
  4609.     -- drop repl_distributor if it exists.
  4610.     if exists (select * from master..sysservers where lower(srvname) = 
  4611.         'repl_distributor' collate database_default)
  4612.     begin
  4613.         exec @retcode = dbo.sp_dropserver 'repl_distributor', 'droplogins'
  4614.         IF @@error <> 0 OR @retcode <> 0
  4615.         BEGIN
  4616.             RETURN(1)
  4617.         END
  4618.     end
  4619.  
  4620.     -- Add the linked server entry for the distributor
  4621.     -- Note we do this even for local server for consistancy
  4622.     EXECUTE @retcode = dbo.sp_addserver  'repl_distributor'
  4623.     IF @@error <> 0 OR @retcode <> 0
  4624.     BEGIN
  4625.         RETURN (1)
  4626.     END
  4627.  
  4628.     select @server_added = 1
  4629.  
  4630.     -- Mark system link
  4631.     EXECUTE @retcode = dbo.sp_serveroption  'repl_distributor', 'system','true'
  4632.     IF @@error <> 0 OR @retcode <> 0
  4633.     BEGIN
  4634.         RETURN (1)
  4635.     END
  4636.  
  4637.     EXECUTE @retcode = dbo.sp_setnetname  'repl_distributor', @distributor
  4638.     IF @@error <> 0 OR @retcode <> 0
  4639.     BEGIN
  4640.         goto UNDO
  4641.     END
  4642.         
  4643.     exec @retcode = dbo.sp_addlinkedsrvlogin 
  4644.         @rmtsrvname= 'repl_distributor',
  4645.         @useself = 'false',
  4646.         @locallogin = NULL,
  4647.         @rmtuser = @login,
  4648.         @rmtpassword = @password
  4649.     IF @@error <> 0 OR @retcode <> 0
  4650.     BEGIN
  4651.         goto UNDO
  4652.     END
  4653.  
  4654.  
  4655.     /*
  4656.     ** If this is not the local server, remote distributor must be set up first
  4657.     */
  4658.     IF UPPER(@distributor) <> UPPER(@@SERVERNAME)
  4659.     BEGIN
  4660.     /* Check distributor version, 7.0 distributor should error out*/
  4661.     declare @dist_ver int
  4662.     select @dist_ver = 0
  4663.     EXEC @retcode = repl_distributor.master.dbo.sp_executesql N'select @dist_ver = @@microsoftversion', 
  4664.                                     N'@dist_ver int output', @dist_ver output
  4665.     IF (@retcode <> 0 or @@ERROR <> 0)
  4666.     begin
  4667.             GOTO UNDO
  4668.     end
  4669.     if (@dist_ver < 0x07320000 )
  4670.     begin
  4671.             RAISERROR (21320,16,-1)
  4672.             GOTO UNDO
  4673.     end
  4674.         /*
  4675.         ** Test to see if the local server is defined as publisher
  4676.         ** at the remove distributor.
  4677.         ** Note: cannot call sp_helpdistributor locally since the server is not
  4678.         ** marked for distribution.
  4679.         ** We can not move the serveroption call before this RPC because RPC failure
  4680.         ** may cause the SP to terminate. Thus, we can not UNDO the server option.
  4681.         */
  4682.         SELECT @distproc = 'repl_distributor.master.dbo.sp_helpdistributor'
  4683.     
  4684.         DECLARE @loc_directory             nvarchar(255)
  4685.         DECLARE @loc_account             nvarchar(255)
  4686.         DECLARE @loc_mindistretention     int
  4687.         DECLARE @loc_maxdistretention     int
  4688.         DECLARE @loc_historyretention   int  
  4689.         DECLARE @loc_historycleanupagent nvarchar(100)
  4690.         DECLARE @loc_distribcleanupagent nvarchar(100)
  4691.         DECLARE @alert_name nvarchar(100)
  4692.         DECLARE @alert_id int
  4693.  
  4694.         EXECUTE @distproc
  4695.             @distributor OUTPUT,
  4696.             @distribution_db OUTPUT,
  4697.             @loc_directory OUTPUT,
  4698.             @loc_account OUTPUT,
  4699.             @loc_mindistretention OUTPUT,
  4700.             @loc_maxdistretention OUTPUT,
  4701.             @loc_historyretention OUTPUT,
  4702.             @loc_historycleanupagent OUTPUT,
  4703.             @loc_distribcleanupagent OUTPUT,
  4704.             @@SERVERNAME,
  4705.             @local = 'local'
  4706.  
  4707.         IF @@error <> 0 OR @retcode <> 0 OR @distribution_db is NULL
  4708.         BEGIN
  4709.             RAISERROR (21007,16,-1)
  4710.             GOTO UNDO
  4711.         END
  4712.  
  4713.         /* Activate the dist publisher at the remote distributor */
  4714.         SELECT @distproc = 'repl_distributor.master.dbo.sp_changedistpublisher'
  4715.             EXECUTE @retcode = @distproc @@SERVERNAME, 'active','true'
  4716.         IF @@error <> 0 OR @retcode <> 0
  4717.         BEGIN
  4718.             GOTO UNDO
  4719.         END
  4720.     END
  4721.     ELSE
  4722.     /* set the registry */
  4723.     BEGIN
  4724.         EXEC @retcode = dbo.sp_MScreate_distributor_tables 
  4725.         if @@error <> 0 or @retcode <> 0
  4726.             goto UNDO
  4727.  
  4728.         declare @distributor_login sysname
  4729.         select @distributor_login = 'distributor_admin'
  4730.  
  4731.         -- Add publisher rpc login
  4732.         if not exists (select * from master..syslogins where loginname = @distributor_login collate database_default)
  4733.         begin
  4734.             EXEC @retcode = dbo.sp_addlogin @loginame = @distributor_login,
  4735.                 @passwd = @password
  4736.             if @@error <> 0 or @retcode <> 0
  4737.                 goto UNDO
  4738.         end
  4739.         else
  4740.         begin
  4741.             -- Change the password if the distributor is local
  4742.             EXEC @retcode = dbo.sp_password NULL, @password, 'distributor_admin'
  4743.             if @@error <> 0 or @retcode <> 0
  4744.                 goto UNDO
  4745.         end
  4746.  
  4747.         -- Add the login to sysadmin
  4748.         -- Refer to sp_MSpublication_access in distribution db
  4749.         if is_srvrolemember('sysadmin', @distributor_login) <> 1
  4750.         begin
  4751.             exec @retcode = dbo.sp_addsrvrolemember @distributor_login, 'sysadmin'
  4752.             IF @@error <> 0 OR @retcode <> 0
  4753.                 GOTO UNDO
  4754.         end
  4755.  
  4756.         if @from_scripting <> 1
  4757.         begin
  4758.             -- Add Replication Agent Checkup Agent
  4759.             exec @retcode = dbo.sp_MScreate_replication_checkup_agent @heartbeat_interval = @heartbeat_interval
  4760.             if @@error <> 0 or @retcode <> 0
  4761.                 goto UNDO
  4762.         end
  4763.         
  4764.         delete msdb..MSdistributor where property = 'heartbeat_interval'
  4765.         if @@error <> 0
  4766.             goto UNDO
  4767.         insert into msdb..MSdistributor (property, value) values ('heartbeat_interval',
  4768.             convert(nvarchar(10), @heartbeat_interval))
  4769.         if @@error <> 0 
  4770.             goto UNDO
  4771.  
  4772.         -- Add Replication Alerts and Response Jobs
  4773.         exec @retcode = dbo.sp_MSadd_distributor_alerts_and_responses
  4774.             @from_scripting = @from_scripting
  4775.         if @@error <> 0 or @retcode <> 0
  4776.             goto UNDO
  4777.     
  4778.     END
  4779.  
  4780.  
  4781.     /*
  4782.     ** Set the server option to indicate that this is a distributor.
  4783.     ** 
  4784.     */
  4785.     EXECUTE @retcode = dbo.sp_serveroption 'repl_distributor', 'dist', true
  4786.     IF @@error <> 0 OR @retcode <> 0
  4787.     BEGIN
  4788.         GOTO UNDO
  4789.     END
  4790.  
  4791.     -- Set sp_MSrepl_startup to be a startup stored procedure
  4792.     -- Note: This needs to be after the marking the distribution server
  4793.     exec @retcode = dbo.sp_procoption 'sp_MSrepl_startup', 'startup', 'true' 
  4794.     if @@error <> 0 or @retcode <> 0
  4795.         goto UNDO1
  4796.  
  4797.     exec @retcode = dbo.sp_MSrepl_startup
  4798.     if @@error <> 0 or @retcode <> 0
  4799.         goto UNDO1
  4800.  
  4801.     RETURN (0)
  4802.  
  4803. UNDO1:
  4804.     exec dbo.sp_serveroption 'repl_distributor', 'dist', false
  4805.  
  4806. UNDO:
  4807.     IF @server_added = 1
  4808.     begin
  4809.         -- Drop the remote logins, otherwise, sp_dropserver will fail.
  4810.         EXECUTE dbo.sp_dropserver 'repl_distributor', 'droplogins'
  4811.     end
  4812.     
  4813.     RETURN (1)
  4814. GO
  4815.  
  4816.  
  4817. raiserror('Creating procedure sp_changedistributor_property', 0,1)
  4818. go
  4819.  
  4820. CREATE PROCEDURE sp_changedistributor_property (
  4821.     @property sysname     = NULL,       /* The property to change */
  4822.     @value nvarchar(255)  = NULL        /* The new property value */
  4823.     ) AS
  4824.  
  4825.     declare @retcode int
  4826.     declare @new_heartbeat_interval int 
  4827.  
  4828.     -- Check to make sure the local server is a distributor
  4829.     if not exists (SELECT * FROM master..sysservers
  4830.               WHERE  srvstatus & 8 <> 0 and
  4831.               UPPER(datasource) = UPPER(@@servername) collate database_default)
  4832.     begin
  4833.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  4834.         return(1)
  4835.     end
  4836.  
  4837.     -- Return list of properties if @property is NULL
  4838.     if @property is NULL
  4839.     begin
  4840.         create table #tab1 (properties sysname collate database_default not null)
  4841.         insert into #tab1 values ('heartbeat_interval')
  4842.         select * from #tab1
  4843.         return (0)
  4844.     end
  4845.  
  4846.     if @property is NULL
  4847.     begin
  4848.         if exists (select name from msdb..sysobjects where name = 'MSdistributor')
  4849.         begin
  4850.             select property from msdb..MSdistributor
  4851.             return 0
  4852.         end
  4853.         return 1
  4854.     end
  4855.  
  4856.     -- Check for valid property
  4857.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) NOT IN ('heartbeat_interval')
  4858.     BEGIN
  4859.         RAISERROR (14154, 16, -1)
  4860.         RETURN (1)
  4861.     END
  4862.  
  4863.     if LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'heartbeat_interval'
  4864.     begin
  4865.         if exists (select name from msdb..sysobjects where name = 'MSdistributor')  
  4866.         begin
  4867.             update msdb..MSdistributor set value = @value where property = @property
  4868.             if @@error <> 0
  4869.                 return 1
  4870.             select @new_heartbeat_interval = CONVERT(int, @value)
  4871.             exec @retcode = dbo.sp_MScreate_replication_checkup_agent @heartbeat_interval = 
  4872.                 @new_heartbeat_interval
  4873.             if @@error <> 0 or @retcode <> 0
  4874.                 return 1
  4875.         end
  4876.         else 
  4877.             return 1
  4878.     end
  4879.  
  4880.     return (0)
  4881.  
  4882. GO
  4883.  
  4884. raiserror('Creating procedure sp_helpdistributor_properties', 0,1)
  4885. go
  4886.  
  4887. CREATE PROCEDURE sp_helpdistributor_properties 
  4888.      AS
  4889.  
  4890.     if exists (select name from msdb..sysobjects where name = 'MSdistributor')
  4891.     begin
  4892.         -- There is currently only one property, so this will work
  4893.         select 'heartbeat_interval' = convert(int, value) from msdb..MSdistributor where
  4894.             property = 'heartbeat_interval'
  4895.         return (0)
  4896.     end
  4897.     return (1)
  4898. GO
  4899.  
  4900. raiserror('Creating procedure sp_dropdistributor', 0,1)
  4901. go
  4902.  
  4903. CREATE PROCEDURE sp_dropdistributor
  4904. @no_checks bit = 0,
  4905. @ignore_distributor bit = 0
  4906. AS
  4907.  
  4908.     SET NOCOUNT ON
  4909.  
  4910.     /*
  4911.     ** Declarations.
  4912.     */
  4913.     DECLARE @retcode int
  4914.     DECLARE @distributor sysname
  4915.     DECLARE @agentname nvarchar(100)
  4916.     DECLARE @distbit int
  4917.     DECLARE @distribdb sysname
  4918.     DECLARE @foundSubscriber int
  4919.     DECLARE @proc nvarchar(255) 
  4920.     declare @optname sysname
  4921.     declare @name sysname
  4922.     DECLARE @transpublishdb_bit int
  4923.     DECLARE @mergepublishdb_bit int
  4924.     declare @job_name nvarchar(100)
  4925.     declare @alert_name nvarchar(100)
  4926.  
  4927.     declare @dist_rpcname sysname
  4928.  
  4929.     declare @alert_id   int
  4930.    
  4931.     SELECT @transpublishdb_bit = 1
  4932.     SELECT @mergepublishdb_bit = 4
  4933.     SELECT @foundSubscriber = 0
  4934.     SELECT @distbit = 16
  4935.  
  4936.     -- Get distributor name
  4937.     select @distributor = datasource, @dist_rpcname = srvname from master..sysservers 
  4938.         WHERE srvstatus & 8 <> 0
  4939.     if @distributor is null
  4940.     BEGIN
  4941.         RAISERROR (21043, 16, -1)
  4942.         RETURN(1)
  4943.     END
  4944.  
  4945.     if @no_checks = 1
  4946.     begin
  4947.         -- We are in bruteforce cleanup mode, drop everything.
  4948.         DECLARE hCdropdistributor CURSOR LOCAL FAST_FORWARD FOR
  4949.                 SELECT name, N'publish' FROM master..sysdatabases      
  4950.                     WHERE (category & @transpublishdb_bit) <> 0 
  4951.                 UNION
  4952.                 select name, N'merge publish' from master..sysdatabases
  4953.                     WHERE (category & @mergepublishdb_bit) <> 0
  4954.                 FOR READ ONLY
  4955.  
  4956.         OPEN hCdropdistributor
  4957.         FETCH hCdropdistributor INTO @name, @optname
  4958.     
  4959.         WHILE (@@fetch_status <> -1)
  4960.         BEGIN
  4961.                         
  4962.             EXECUTE @retcode  = dbo.sp_replicationdboption 
  4963.               @dbname = @name,
  4964.               @optname = @optname,
  4965.               @value = 'false',
  4966.               @ignore_distributor = @ignore_distributor
  4967.             
  4968.             IF @@ERROR <> 0 OR @retcode <> 0
  4969.             BEGIN
  4970.                 CLOSE hCdropdistributor
  4971.                 DEALLOCATE hCdropdistributor
  4972.                 RETURN (1)
  4973.             END
  4974.             FETCH hCdropdistributor INTO @name, @optname
  4975.         end
  4976.  
  4977.         CLOSE hCdropdistributor
  4978.         DEALLOCATE hCdropdistributor
  4979.     
  4980.         -- Drop subscriber
  4981.         EXECUTE @retcode  = dbo.sp_dropsubscriber
  4982.           @subscriber = 'all',
  4983.           @ignore_distributor = @ignore_distributor
  4984.  
  4985.         IF @@ERROR <> 0 OR @retcode <> 0
  4986.             RETURN (1)
  4987.  
  4988.         IF UPPER(@distributor) = UPPER(@@SERVERNAME) 
  4989.         begin
  4990.             -- Clean up dist publishers
  4991.             DECLARE hCdropdistributor CURSOR LOCAL FAST_FORWARD FOR
  4992.                     SELECT name FROM msdb..MSdistpublishers
  4993.             FOR READ ONLY
  4994.  
  4995.             OPEN hCdropdistributor
  4996.             FETCH hCdropdistributor INTO @name
  4997.  
  4998.             WHILE (@@fetch_status <> -1)
  4999.             BEGIN
  5000.                 exec @retcode = dbo.sp_dropdistpublisher
  5001.                     @publisher = @name,
  5002.                     @no_checks = 1
  5003.             
  5004.                 IF @@ERROR <> 0 OR @retcode <> 0
  5005.                 BEGIN
  5006.                     CLOSE hCdropdistributor
  5007.                     DEALLOCATE hCdropdistributor
  5008.                     RETURN (1)
  5009.                 END
  5010.                 FETCH hCdropdistributor INTO @name
  5011.             end
  5012.  
  5013.             CLOSE hCdropdistributor
  5014.             DEALLOCATE hCdropdistributor
  5015.  
  5016.             -- Clean up distribution dbs
  5017.             DECLARE hCdropdistributor CURSOR LOCAL FAST_FORWARD FOR
  5018.                     SELECT name FROM msdb..MSdistributiondbs
  5019.             FOR READ ONLY
  5020.  
  5021.             OPEN hCdropdistributor
  5022.             FETCH hCdropdistributor INTO @name
  5023.     
  5024.             WHILE (@@fetch_status <> -1)
  5025.             BEGIN
  5026.                 exec @retcode = dbo.sp_dropdistributiondb
  5027.                     @database = @name
  5028.  
  5029.                 IF @@ERROR <> 0 OR @retcode <> 0
  5030.                 BEGIN
  5031.                     CLOSE hCdropdistributor
  5032.                     DEALLOCATE hCdropdistributor
  5033.                     RETURN (1)
  5034.                 END
  5035.                 FETCH hCdropdistributor INTO @name
  5036.             end
  5037.  
  5038.             CLOSE hCdropdistributor
  5039.             DEALLOCATE hCdropdistributor
  5040.         end
  5041.     end
  5042.     
  5043.     -- If everything should be cleaned up when we reach here with @no_checks = 1
  5044.     /*
  5045.     ** If local distributor, check if there are any distributor databases
  5046.     */
  5047.     IF UPPER(@distributor) = UPPER(@@SERVERNAME) 
  5048.     BEGIN
  5049.         IF EXISTS (SELECT * FROM msdb..MSdistributiondbs)
  5050.         BEGIN
  5051.             RAISERROR (14121, 16, -1, @distributor)
  5052.             RETURN(1)
  5053.         END
  5054.     END
  5055.     ELSE
  5056.     begin
  5057.         -- Check to see if there are database published.
  5058.         if exists (SELECT * FROM master..sysdatabases      
  5059.             WHERE (category & @transpublishdb_bit) <> 0 or 
  5060.             (category & @mergepublishdb_bit) <> 0)
  5061.         begin
  5062.             raiserror(21045, 16, -1)
  5063.             return(1)
  5064.         end
  5065.  
  5066.         -- Check to see if there are subscribers defined.
  5067.         if exists (SELECT *
  5068.                      FROM master..sysservers
  5069.                     WHERE srvstatus & 4 <> 0)
  5070.         begin
  5071.             raiserror(21008, 16, -1)
  5072.             return(1)
  5073.         end
  5074.     end
  5075.  
  5076.  
  5077.     /*
  5078.     ** if @ignore_distributor = 1, we are in bruteforce cleanup mode, don't do RPC.
  5079.     */
  5080.     if @ignore_distributor = 0 
  5081.     begin
  5082.         /*
  5083.         ** Get distribution server information
  5084.         */
  5085.         EXEC @retcode = dbo.sp_helpdistributor @publisher = @@SERVERNAME,
  5086.             @distribdb = @distribdb OUTPUT
  5087.         IF @@error <> 0 OR @retcode <> 0
  5088.         BEGIN
  5089.             RAISERROR (14071, 16, -1)
  5090.             RETURN (1)
  5091.         END
  5092.  
  5093.         IF @distribdb is NOT NULL
  5094.         BEGIN
  5095.             /* 
  5096.             ** Deactivate the dist publisher at the  distributor 
  5097.             ** Only do this if @distribdb is NOT NULL, which means the dist publisher
  5098.             ** if defined.
  5099.             */
  5100.             SELECT @proc = RTRIM(@dist_rpcname) + '.master.dbo.sp_changedistpublisher'
  5101.                 EXECUTE @retcode = @proc @@SERVERNAME, 'active','false'
  5102.             IF @@error <> 0 OR @retcode <> 0
  5103.             BEGIN
  5104.                 RETURN (1)
  5105.             END
  5106.         END
  5107.     end
  5108.  
  5109.  
  5110.     /*
  5111.     ** Clear the server option to indicate that this is a distributor.
  5112.     */
  5113.     EXECUTE @retcode = dbo.sp_serveroption @dist_rpcname, 'dist', false
  5114.     IF @@error <> 0 OR @retcode <> 0
  5115.         BEGIN
  5116.             RETURN(1)
  5117.         END
  5118.  
  5119.     -- Prevent dropping local server entry accidentally if user 
  5120.     -- set 'dist' server option on local server.
  5121.     if UPPER(@dist_rpcname) <> UPPER(@@servername)
  5122.     begin
  5123.         exec @retcode = dbo.sp_dropserver @dist_rpcname, 'droplogins'
  5124.         IF @@error <> 0 OR @retcode <> 0
  5125.         BEGIN
  5126.             RETURN(1)
  5127.         END
  5128.     end
  5129.  
  5130.  
  5131.     -- Drop table after unmark distributor to prevent 
  5132.     -- sp_helpdist* failures.
  5133.  
  5134.     /* If local, Drop replication category and alerts */
  5135.     IF UPPER(@distributor) = UPPER(@@SERVERNAME)
  5136.     BEGIN
  5137.  
  5138.     -- Attempts to drop distributor_login, no big deal if it's still in use and we can't drop, don't quit here.
  5139.  
  5140.            declare @distributor_login sysname
  5141.         select @distributor_login = 'distributor_admin'
  5142.  
  5143.         if exists (select * from master..syslogins where loginname = @distributor_login collate database_default)
  5144.         begin
  5145.             EXEC @retcode = dbo.sp_droplogin @loginame = @distributor_login
  5146.     end
  5147.  
  5148.         -- Drop Distributor Alerts and Jobs
  5149.         exec @retcode = dbo.sp_MSdrop_distributor_alerts_and_responses
  5150.         IF @@ERROR <> 0 or @retcode <> 0
  5151.         BEGIN
  5152.             return (1)            
  5153.         END
  5154.  
  5155.         -- Drop the two system tables.
  5156.         if exists (select * from msdb..sysobjects where name = 'MSdistpublishers'
  5157.             and xtype = 'U')
  5158.             drop table msdb..MSdistpublishers
  5159.     
  5160.         if @@error <> 0 
  5161.             return 1 ;
  5162.  
  5163.         if exists (select * from msdb..sysobjects where name = 'MSdistributiondbs'
  5164.             and xtype = 'U')
  5165.             drop table msdb..MSdistributiondbs
  5166.  
  5167.         if @@error <> 0
  5168.             return 1 ;
  5169.  
  5170.         if exists (select * from msdb..sysobjects where name = 'MSdistributor'
  5171.                 and xtype = 'U')
  5172.             drop table msdb..MSdistributor
  5173.  
  5174.         if @@error <> 0
  5175.             return 1 ;
  5176.  
  5177.         if exists (select * from msdb..sysobjects where name = 'sysreplicationalerts'
  5178.                 and xtype = 'U')
  5179.             drop table msdb..sysreplicationalerts
  5180.  
  5181.         if @@error <> 0
  5182.             return 1 ;
  5183.  
  5184.  
  5185.         if exists (select * from msdb..sysobjects where name = 'MSagent_profiles'
  5186.             and xtype = 'U')
  5187.             drop table msdb..MSagent_profiles
  5188.  
  5189.         if @@error <> 0
  5190.             return 1 ;
  5191.  
  5192.  
  5193.        if exists (select * from msdb..sysobjects where name = 'MSagent_parameters'
  5194.                 and xtype = 'U')
  5195.             drop table msdb..MSagent_parameters
  5196.  
  5197.         if @@error <> 0
  5198.             return 1 ;
  5199.  
  5200.     END
  5201.  
  5202.     -- Nnregister sp_MSrepl_startup as a startup stored procedure
  5203.     exec @retcode = dbo.sp_procoption 'sp_MSrepl_startup', 'startup', 'false' 
  5204.     if @@error <> 0 or @retcode <> 0
  5205.         return 1
  5206.  
  5207.     RETURN (0)        
  5208. GO
  5209.  
  5210. dump tran master with NO_LOG
  5211. go
  5212.  
  5213. raiserror('Creating procedure sp_helpdistributiondb', 0,1)
  5214. go
  5215.  
  5216. CREATE PROCEDURE sp_helpdistributiondb (
  5217.     @database sysname = '%'
  5218.     ) AS
  5219.  
  5220.     SET NOCOUNT ON
  5221.  
  5222.     declare @retcode int
  5223.     declare @cmd    nvarchar(500)
  5224.  
  5225.     /*
  5226.     ** Check to make sure this is a distributor
  5227.     */
  5228.     IF NOT EXISTS (select * from master..sysservers
  5229.               where UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  5230.                  AND srvstatus & 8 <> 0)
  5231.     begin
  5232.     if @database <> '%' 
  5233.     begin
  5234.             raiserror (14114, 16, -1, @@SERVERNAME)
  5235.             return(1)
  5236.     end
  5237.     else
  5238.        return(0)
  5239.     end
  5240.  
  5241.     IF NOT EXISTS (select * from msdb..sysobjects where name = 'MSdistributiondbs' and type = 'U')
  5242.     begin
  5243.         raiserror (14071, 16, -1)
  5244.         return(1)
  5245.     end    
  5246.  
  5247.     /*
  5248.     ** Check if database is configured as a distributor database
  5249.     */
  5250.     IF @database <> '%' AND NOT EXISTS (select * from msdb..MSdistributiondbs where name = @database collate database_default)
  5251.     begin
  5252.         raiserror (14117, 16, -1, @database)
  5253.         return(1)
  5254.     end    
  5255.  
  5256.     -- Begin from sp_helpdb
  5257.  
  5258.     /*  Create temp table before any DMP to enure dynamic
  5259.     **  Since we examine the status bits in sysdatabase and turn them
  5260.     **  into english, we need a temporary table to build the descriptions.
  5261.     */
  5262.     create table #spdbdesc
  5263.     (
  5264.         dbid    smallint    null,
  5265.         dbdesc  nvarchar(175)   collate database_default null
  5266.     )
  5267.     if @@error<>0
  5268.     begin
  5269.         return(1)
  5270.     end
  5271.  
  5272.     /* 
  5273.     ** Since we need to execute dynamic SQL to get the list of files, create a temp 
  5274.     ** table to keep file descriptions
  5275.     */
  5276.     create table #spfiledesc
  5277.     (
  5278.         dbid    smallint        not null,
  5279.         fileid  smallint        not null,
  5280.         status  int        not null,
  5281.         size    int             not null,
  5282.         name    sysname         collate database_default not null,
  5283.         filename nvarchar(260)   collate database_default not null
  5284.     )   
  5285.     if @@error<>0
  5286.     begin
  5287.         return(1)
  5288.     end
  5289.  
  5290.     /*
  5291.     **  Initialize #spdbdesc from sysdatabases
  5292.     */
  5293.     insert into #spdbdesc (dbid)
  5294.             select dbid 
  5295.               from master.dbo.sysdatabases d 
  5296.         inner join msdb.dbo.MSdistributiondbs dist
  5297.                 on dist.name = d.name collate database_default 
  5298.              where dist.name like @database collate database_default
  5299.                and has_dbaccess(d.name) = 1
  5300.  
  5301.     declare @name sysname
  5302.  
  5303.     /* 
  5304.     ** Insert the list of all files into #spfiledesc
  5305.     */
  5306.     declare c1 CURSOR LOCAL FAST_FORWARD FOR 
  5307.         select db_name (dbid) from #spdbdesc
  5308.     open c1
  5309.     fetch c1 into @name
  5310.     while @@fetch_status >= 0
  5311.     begin
  5312.  
  5313.         /* Insert row for each database */
  5314.         select @cmd =  ('insert into #spfiledesc
  5315.                 select db_id (N'+ QUOTENAME(@name,'''') + '), fileid, status, size, RTRIM(name), RTRIM(filename) from' 
  5316.                 + ' ' + QUOTENAME(@name) + '.dbo.sysfiles')
  5317.         exec (@cmd)
  5318.         fetch c1 into @name
  5319.     end
  5320.     deallocate c1
  5321.  
  5322.     SELECT dist.name, min_distretention, max_distretention, history_retention,
  5323.         'history_cleanup_agent' =  formatmessage (20567, dist.name),
  5324.         'distributor_cleanup_agent' = formatmessage(20568, dist.name),
  5325.         'status'= 0, 
  5326.  
  5327.         -- Refer to sp_helpdb for the following query.
  5328.         'data_folder' = substring(v_data.filename, 1, len(v_data.filename) - charindex('\', reverse(v_data.filename))),
  5329.         'data_file' = right(v_data.filename, charindex('\', reverse(v_data.filename))-1),
  5330.         'data_file_size' = CONVERT(int, (select low from master.dbo.spt_values
  5331.             where type = 'E'
  5332.                 and number = 1)
  5333.             * convert(dec(15), v_data.size)/ 1048576),
  5334.     
  5335.         'log_folder' = substring(v_log.filename, 1, len(v_log.filename) - charindex('\', reverse(v_log.filename))),
  5336.         'log_file' = right(v_log.filename, charindex('\', reverse(v_log.filename))-1),
  5337.         'log_file_size' = CONVERT(int, (select low from master.dbo.spt_values
  5338.             where type = 'E'
  5339.                 and number = 1)
  5340.             * convert(dec(15), v_log.size)/ 1048576)
  5341.         
  5342.         FROM msdb.dbo.MSdistributiondbs dist,
  5343.         master.dbo.sysdatabases d, #spfiledesc v_data,
  5344.         #spfiledesc v_log
  5345.         WHERE dist.name LIKE @database collate database_default
  5346.             
  5347.             and v_data.dbid = d.dbid 
  5348.             and v_data.dbid > 0 
  5349.             and v_data.status & 2 = 2
  5350.             and v_data.status & 0x40 = 0 -- data
  5351.             and v_data.fileid = (select min(v_data2.fileid) from #spfiledesc v_data2 where
  5352.                     v_data2.dbid = d.dbid 
  5353.                 and v_data2.dbid > 0    
  5354.                 and v_data2.status & 2 = 2
  5355.                 and v_data2.status & 0x40 = 0 ) -- data
  5356.  
  5357.             
  5358.             and v_log.dbid = d.dbid 
  5359.             and v_log.dbid > 0  
  5360.             and v_log.status & 2 = 2
  5361.             and v_log.status & 0x40 = 0x40 -- log
  5362.             and v_log.fileid = (select min(v_log2.fileid) from #spfiledesc v_log2 where
  5363.                     v_log2.dbid = d.dbid 
  5364.                 and v_log2.dbid > 0 
  5365.                 and v_log2.status & 2 = 2
  5366.                 and v_log2.status & 0x40 = 0x40 ) -- log
  5367.  
  5368.             and d.name = dist.name collate database_default
  5369. GO
  5370.  
  5371.  
  5372. dump tran master with no_log
  5373. GO
  5374.  
  5375. raiserror('Creating procedure sp_changedistributiondb', 0,1)
  5376. go
  5377.  
  5378. CREATE PROCEDURE sp_changedistributiondb (
  5379.     @database sysname,
  5380.     @property sysname     = NULL,     /* The property to change */
  5381.     @value nvarchar(255)     = NULL      /* The new property value */
  5382.     ) AS
  5383.  
  5384.     SET NOCOUNT ON
  5385.  
  5386.     /*
  5387.     ** Declarations.
  5388.     */
  5389.     DECLARE @distributor sysname
  5390.     DECLARE @min_distretention     int
  5391.     DECLARE @max_distretention     int
  5392.     DECLARE @retcode             int
  5393.     DECLARE @new_min_distretention     int
  5394.     DECLARE @new_max_distretention     int
  5395.     DECLARE @new_history_retention     int
  5396.     DECLARE @agentname nvarchar(100)
  5397.     DECLARE @command nvarchar(255)
  5398.     DECLARE @security_mode int
  5399.     DECLARE @distbit int
  5400.  
  5401.     SELECT @distbit = 16
  5402.  
  5403.     /*
  5404.     ** Parameter Check:  @property.
  5405.     ** If the @property parameter is NULL, print the options.
  5406.     */
  5407.  
  5408.     IF @property IS NULL
  5409.         BEGIN
  5410.             CREATE TABLE #tab1 (properties sysname collate database_default not null)
  5411.             INSERT INTO #tab1 VALUES ('min_distretention')
  5412.             INSERT INTO #tab1 VALUES ('max_distretention')
  5413.             INSERT INTO #tab1 VALUES ('history_retention')
  5414.             SELECT * FROM #tab1
  5415.             RETURN (0)
  5416.         END
  5417.  
  5418.     /*
  5419.     ** Parameter Check:  @property.
  5420.     ** Check to make sure that @property is a valid property in
  5421.     ** sysarticles.
  5422.     */
  5423.     IF @property IS NULL OR LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) NOT IN 
  5424.                                                     ('min_distretention',
  5425.                                                      'max_distretention',
  5426.                                                      'history_retention')
  5427.              
  5428.         BEGIN
  5429.  
  5430.             RAISERROR (14115, 16, -1, 
  5431.             '''min_distretention'', ''max_distretention'' or ''history_retention''')
  5432.             RETURN (1)
  5433.         END
  5434.  
  5435.  
  5436.     /*
  5437.     ** Check to make sure this is a distributor
  5438.     */
  5439.     IF NOT EXISTS (SELECT * FROM master..sysservers
  5440.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  5441.                  AND srvstatus & 8 <> 0)
  5442.     BEGIN
  5443.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  5444.         RETURN(1)
  5445.     END
  5446.  
  5447.     /*
  5448.     ** Check if database is configured as a distributor database
  5449.     */
  5450.     IF NOT EXISTS (SELECT * FROM master..sysdatabases
  5451.               WHERE name = @database collate database_default
  5452.                  AND category & @distbit <> 0)
  5453.     BEGIN
  5454.         RAISERROR (14117, 16, -1, @database)
  5455.         RETURN(1)
  5456.     END
  5457.     
  5458.     /*
  5459.     ** Change the property.
  5460.     */
  5461.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'min_distretention'
  5462.         BEGIN
  5463.             IF @value IS NULL
  5464.                 BEGIN
  5465.                     RAISERROR (14043, 16, -1, '@value') 
  5466.                     RETURN (1)
  5467.                 END
  5468.  
  5469.             /*
  5470.             ** Set the MinDistRetention registry key value
  5471.             */
  5472.             SELECT @new_min_distretention = CONVERT(int, @value)
  5473.  
  5474.             /*
  5475.             ** Get MaxDistRetention value
  5476.             */
  5477.             SELECT @max_distretention = max_distretention FROM msdb..MSdistributiondbs
  5478.                 WHERE name = @database collate database_default
  5479.                 
  5480.             /* 
  5481.             ** Check for invalid retention values 
  5482.             */
  5483.             IF @new_min_distretention < 0
  5484.                 BEGIN
  5485.                        RAISERROR(14106, 16, -1)
  5486.                     RETURN (1)
  5487.                 END
  5488.             IF @new_min_distretention > @max_distretention
  5489.                 BEGIN
  5490.                     RAISERROR(14107, 16, -1) 
  5491.                     RETURN (1)
  5492.                 END
  5493.  
  5494.             UPDATE msdb..MSdistributiondbs SET min_distretention = @new_min_distretention
  5495.                 WHERE name = @database collate database_default
  5496.             IF @@error <> 0 
  5497.                 BEGIN
  5498.                     RETURN (1)
  5499.                 END
  5500.  
  5501.             /*
  5502.             ** Update Distribution Cleanup agent 
  5503.             */
  5504.             select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  5505.             j.job_id = s.job_id and
  5506.             j.category_id = 11 and
  5507.             s.database_name = @database collate database_default
  5508.         
  5509.             SELECT @command =  'EXEC dbo.sp_MSdistribution_cleanup @min_distretention = ' + 
  5510.                 CONVERT(nvarchar(12), @new_min_distretention) + ', @max_distretention = ' +
  5511.                 CONVERT(nvarchar(12), @max_distretention)
  5512.             EXEC @retcode = msdb.dbo.sp_update_jobstep @job_name = @agentname, @step_id = 1, 
  5513.                 @command = @command
  5514.             IF @@error <> 0 OR @retcode <> 0
  5515.                RETURN(1)
  5516.  
  5517.         END
  5518.  
  5519.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'max_distretention'
  5520.         BEGIN
  5521.             IF @value IS NULL
  5522.                 BEGIN
  5523.                     RAISERROR (14043, 16, -1, '@value') 
  5524.                     RETURN (1)
  5525.                 END
  5526.  
  5527.             /*
  5528.             ** Set the MaxDistRetention registry key value
  5529.             */
  5530.             SELECT @new_max_distretention = CONVERT(int, @value)
  5531.  
  5532.             /*
  5533.             ** Get MinDistRetention value
  5534.             */
  5535.             SELECT @min_distretention = min_distretention FROM msdb..MSdistributiondbs
  5536.                 WHERE name = @database collate database_default
  5537.  
  5538.             /* 
  5539.             ** Check for invalid retention values 
  5540.             */
  5541.             IF @new_max_distretention < 0
  5542.                 BEGIN
  5543.                        RAISERROR(14106, 16, -1)
  5544.                     RETURN (1)
  5545.                 END
  5546.             IF @new_max_distretention < @min_distretention
  5547.                 BEGIN
  5548.                     RAISERROR(14107, 16, -1) 
  5549.                     RETURN (1)
  5550.                 END
  5551.  
  5552.             UPDATE msdb..MSdistributiondbs SET max_distretention = @new_max_distretention
  5553.                 WHERE name = @database collate database_default
  5554.             IF @@error <> 0 
  5555.                 BEGIN
  5556.                     RETURN (1)
  5557.                 END
  5558.             /*
  5559.             ** Update Distribution Cleanup agent 
  5560.             */
  5561.             select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  5562.                 j.job_id = s.job_id and
  5563.                 j.category_id = 11 and
  5564.                 s.database_name = @database collate database_default
  5565.             SELECT @command =  'EXEC dbo.sp_MSdistribution_cleanup @min_distretention = ' + 
  5566.                 CONVERT(nvarchar(12), @min_distretention) + ', @max_distretention = ' +
  5567.                 CONVERT(nvarchar(12), @new_max_distretention)
  5568.             EXEC @retcode = msdb.dbo.sp_update_jobstep @job_name = @agentname, @step_id = 1, 
  5569.                 @command = @command
  5570.             IF @@error <> 0 OR @retcode <> 0
  5571.                RETURN(1)
  5572.         END
  5573.  
  5574.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'history_retention'
  5575.         BEGIN
  5576.             IF @value IS NULL
  5577.                 BEGIN
  5578.                     RAISERROR (14043, 16, -1, '@value') 
  5579.                     RETURN (1)
  5580.                 END
  5581.  
  5582.             /*
  5583.             ** Set the HistoryRetention registry key value
  5584.             */
  5585.             SELECT @new_history_retention = CONVERT(int, @value)
  5586.  
  5587.             UPDATE msdb..MSdistributiondbs SET history_retention = @new_history_retention
  5588.                 WHERE name = @database collate database_default
  5589.             IF @@error <> 0 
  5590.                 BEGIN
  5591.                     RETURN (1)
  5592.                 END
  5593.  
  5594.             /*
  5595.             ** Update History Cleanup agent 
  5596.             */
  5597.             select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  5598.                 j.job_id = s.job_id and
  5599.                 j.category_id = 12 and
  5600.                 s.database_name = @database collate database_default
  5601.             SELECT @command =  'EXEC dbo.sp_MShistory_cleanup @history_retention = ' + 
  5602.                 CONVERT(nvarchar(12), @new_history_retention)
  5603.             EXEC @retcode = msdb.dbo.sp_update_jobstep @job_name = @agentname, @step_id = 1, 
  5604.                 @command = @command
  5605.             IF @@error <> 0 OR @retcode <> 0
  5606.                RETURN(1)
  5607.  
  5608.         END
  5609.  
  5610.  
  5611.  
  5612.  
  5613.     /*
  5614.     ** Return succeed.
  5615.     */
  5616.     RAISERROR (14105, 10, -1, @property)
  5617.     RETURN (0)
  5618. go
  5619.  
  5620. raiserror('Creating procedure sp_dropdistributiondb', 0,1)
  5621. go
  5622.  
  5623. CREATE PROCEDURE sp_dropdistributiondb (
  5624.     @database sysname        
  5625.     
  5626. ) AS
  5627.  
  5628.     SET NOCOUNT ON
  5629.     /*
  5630.     ** Declarations.
  5631.     */
  5632.     DECLARE @retcode int
  5633.     DECLARE @agentname nvarchar(100)
  5634.     DECLARE @reg_key nvarchar(255)
  5635.     DECLARE @security_mode int
  5636.     DECLARE    @distbit int
  5637.     DECLARE @distpublisher sysname
  5638.     DECLARE @distdb sysname
  5639.     DECLARE @device_name sysname
  5640.     DECLARE @command nvarchar(255)
  5641.  
  5642.     SELECT @distbit = 16
  5643.  
  5644.     /*
  5645.     ** Check to make sure this is a distributor
  5646.     */
  5647.     IF NOT EXISTS (SELECT * FROM master..sysservers
  5648.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  5649.                  AND srvstatus & 8 <> 0)
  5650.     BEGIN
  5651.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  5652.         RETURN(1)
  5653.     END
  5654.  
  5655.     /*
  5656.     ** Check if database is configured as a distributor database
  5657.     */
  5658.     IF NOT EXISTS (SELECT * FROM msdb..MSdistributiondbs WHERE name = @database collate database_default)
  5659.     BEGIN
  5660.         RAISERROR (14117, 16, -1, @database)
  5661.         RETURN(1)
  5662.     END
  5663.  
  5664.     /*
  5665.     ** Check if any DistPublishers are using this database
  5666.     */
  5667.     IF EXISTS (SELECT * FROM msdb..MSdistpublishers WHERE
  5668.         distribution_db = @database collate database_default)
  5669.     BEGIN
  5670.         RAISERROR (14120, 16, -1, @database)
  5671.         RETURN (1)
  5672.     END
  5673.     
  5674.     /*
  5675.     ** Check if the DB is being currently used
  5676.     */
  5677.     IF EXISTS (SELECT * FROM master..sysprocesses WHERE
  5678.         dbid = DB_ID(@database))
  5679.     BEGIN
  5680.         RAISERROR (21122, 16, 1, @database)
  5681.         RETURN (1)
  5682.     END
  5683.        
  5684.  
  5685.     /*
  5686.     ** Drop the history cleanup agent.
  5687.     */
  5688.     -- Get agent name
  5689.     select @agentname = null
  5690.     select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  5691.         j.job_id = s.job_id and
  5692.         j.category_id = 12 and
  5693.         s.database_name = @database collate database_default
  5694.  
  5695.     if @agentname is not null
  5696.     BEGIN
  5697.         EXEC @retcode = msdb.dbo.sp_delete_job 
  5698.             @job_name = @agentname
  5699.         IF @@ERROR <> 0 or @retcode <> 0
  5700.         BEGIN
  5701.             RETURN(1)
  5702.         END
  5703.     END
  5704.  
  5705.     /*
  5706.     ** Drop the distribution cleanup agent.
  5707.     */
  5708.     select @agentname = null
  5709.     select @agentname = name from msdb..sysjobs j, msdb..sysjobsteps s where 
  5710.                 j.job_id = s.job_id and
  5711.                 j.category_id = 11 and
  5712.                 s.database_name = @database collate database_default
  5713.     if @agentname is not null
  5714.     BEGIN
  5715.         EXEC @retcode = msdb.dbo.sp_delete_job 
  5716.             @job_name = @agentname
  5717.         IF @@ERROR <> 0 or @retcode <> 0
  5718.         BEGIN
  5719.             RETURN(1)
  5720.         END
  5721.     END
  5722.  
  5723.     /*
  5724.     ** Update sysdatabase category bit before dropping the database
  5725.     ** Otherwise, the database can not be dropped.
  5726.     **
  5727.     */
  5728.     UPDATE master..sysdatabases SET category = category & ~@distbit WHERE name = @database collate database_default
  5729.     IF @@ERROR <> 0
  5730.         BEGIN
  5731.             RETURN (1)
  5732.         END
  5733.  
  5734.  
  5735.     -- Drop the distributor db if it exists
  5736.     IF EXISTS (SELECT * FROM master..sysdatabases WHERE name = @database collate database_default)
  5737.     BEGIN
  5738.  
  5739.         CREATE TABLE #db_existed
  5740.         (
  5741.         db_existed bit  NOT NULL
  5742.         )
  5743.  
  5744.         /* Get version stamp */
  5745.         SELECT @command = 'INSERT INTO #db_existed SELECT db_existed FROM ' +
  5746.             QUOTENAME(@database) + '..MSrepl_version'
  5747.     
  5748.         EXEC(@command)
  5749.         IF @@ERROR <> 0
  5750.         BEGIN
  5751.             RETURN(1)       
  5752.         END
  5753.  
  5754.         -- Drop the distribution db only if it is created in sp_adddistributiondb.
  5755.         IF NOT EXISTS (SELECT * FROM #db_existed where db_existed = 0x1)
  5756.         BEGIN
  5757.     
  5758.             -- we're dropping the database, so don't bother to clean up
  5759.             -- individual records.  Just make sure we blow away the synchronization
  5760.             -- files & directories hosted by the file system.
  5761.  
  5762.             SELECT @command = QUOTENAME(@database) + '.dbo.sp_MSdrop_snapshot_dirs'
  5763.             SELECT @command = 'EXEC ' + @command
  5764.             EXEC (@command)
  5765.             IF (@@ERROR <> 0)
  5766.             BEGIN
  5767.                 RETURN (1)
  5768.             END
  5769.  
  5770.             /* Save the device info before dropping the database */
  5771.             CREATE TABLE #distdbdevices (name sysname  collate database_default not null)
  5772.  
  5773.             /* Drop distributor devices */
  5774.             /* Query is copied from sp_helpdb */
  5775.             SELECT @command = 
  5776.                 'INSERT INTO #distdbdevices select DISTINCT name from ' + QUOTENAME(@database) + '.dbo.sysfiles'
  5777.             EXEC (@command)
  5778.             IF @@ERROR <>0
  5779.             BEGIN
  5780.                 RETURN(1)      
  5781.             END
  5782.  
  5783.             /*
  5784.             ** Drop the distribution database
  5785.             */
  5786.             SELECT @command = 'drop database ' + QUOTENAME(@database)
  5787.             EXEC (@command)
  5788.             IF @@ERROR <> 0
  5789.                 BEGIN
  5790.                     /* Mark the database as distribution database again 
  5791.                     ** Otherwise, this sp will fail when it is reentered.
  5792.                     ** It is often the case that if there's an open session
  5793.                     ** on the database, it can not be dropped.
  5794.                     */
  5795.                     RETURN(1)
  5796.                 END
  5797.  
  5798.             /* Drop distributor devices */
  5799.             /* Query is copied from sp_helpdb */
  5800.             /* SQL SERVER 7.0 may drop some auto generated device files, drop drop them here
  5801.                again
  5802.             */
  5803.             DECLARE hCdropdistributiondb CURSOR LOCAL FAST_FORWARD FOR
  5804.                 SELECT DISTINCT dd.name FROM #distdbdevices dd, master.dbo.sysdevices ss
  5805.                     WHERE ss.name = dd.name collate database_default
  5806.             FOR READ ONLY
  5807.     
  5808.             OPEN hCdropdistributiondb
  5809.             FETCH hCdropdistributiondb INTO @device_name
  5810.  
  5811.             WHILE (@@fetch_status <> -1)
  5812.             BEGIN
  5813.                 /* 
  5814.                 ** Device may be used by other databases
  5815.                 ** Ignore all errors 
  5816.                 */
  5817.                 EXEC dbo.sp_dropdevice @device_name, DELFILE
  5818.                 FETCH hCdropdistributiondb INTO @device_name
  5819.             END
  5820.  
  5821.             CLOSE hCdropdistributiondb
  5822.             DEALLOCATE hCdropdistributiondb
  5823.  
  5824.         END
  5825.         -- else, database did exist before it was made the distributor, 
  5826.         -- do full cleanup
  5827.         ELSE
  5828.         BEGIN
  5829.  
  5830.             -- run 'fast clean' routine first. this removes repldata files
  5831.             -- and truncates MSrepl_transactions/commands
  5832.  
  5833.             SELECT @command = QUOTENAME(@database) + '.dbo.sp_MSfast_delete_trans'
  5834.             SELECT @command = 'EXEC ' + @command
  5835.             EXEC (@command)
  5836.             IF (@@ERROR <> 0)
  5837.             BEGIN
  5838.                 RETURN (1)
  5839.             END
  5840.  
  5841.             -- Do distribution cleanup the final time
  5842.             -- this handles those fiddly agent tables & such
  5843.             -- ( and would delete commands/xacts if we hadn't already purged 'em )
  5844.  
  5845.             SELECT @command = QUOTENAME(@database) + 
  5846.                 '.dbo.sp_MSdistribution_cleanup @min_distretention = 0, @max_distretention = 0'
  5847.             SELECT @command = 'EXEC ' + @command
  5848.             EXEC (@command)
  5849.             IF (@@ERROR <> 0)
  5850.             BEGIN
  5851.                 RETURN (1)
  5852.             END
  5853.         END
  5854.     END
  5855.     
  5856.  
  5857.     /*
  5858.     ** Delete the DistributionDB entry
  5859.     */
  5860.  
  5861.     DELETE msdb.dbo.MSdistributiondbs where name = @database collate database_default
  5862.     IF @@error <> 0
  5863.     BEGIN
  5864.         RETURN(1)
  5865.     END
  5866.     
  5867.  
  5868.     RETURN (0)
  5869.                 
  5870. GO
  5871.  
  5872. dump tran master with no_log
  5873. GO
  5874. raiserror('Creating procedure sp_adddistributiondb', 0,1)
  5875. go
  5876.  
  5877. CREATE PROCEDURE sp_adddistributiondb (
  5878.     @database sysname,
  5879.     @data_folder nvarchar(255) = NULL,
  5880.     @data_file nvarchar(255) = NULL,            /* physical file name */
  5881.     @data_file_size int = 2,                    /* Default: 2MB */            
  5882.     @log_folder nvarchar(255) = NULL,
  5883.     @log_file nvarchar(255) = NULL,             /* physical file name */
  5884.     @log_file_size int = 0,
  5885.     @min_distretention int = 0,                 /* min distribution retention period in hours */
  5886.     @max_distretention int = 72,                /* max distribution retention period in hours */
  5887.     @history_retention int = 48,                /* history retention period in hours */
  5888.     @security_mode int = 0,                     /* distributor login security 0 standard 1 integrated */
  5889.     @login sysname = 'sa',                      /* standard login name */
  5890.     @password sysname = NULL,                   /* standard login password */
  5891.     @createmode int = 0,  /* 0: use create db for attach (recommended), 
  5892.                             1: create db or use existing but no attach (this is the old way), 
  5893.                             2: create for instdist and detach only */
  5894.     @from_scripting bit = 0
  5895.     ) AS
  5896.  
  5897.     SET NOCOUNT ON
  5898.  
  5899.     /*
  5900.     ** Declarations.
  5901.     */
  5902.     DECLARE @data_path nvarchar(512)
  5903.     DECLARE @log_path nvarchar(512)
  5904.     
  5905.     DECLARE @data_path_quoted_for_copy nvarchar(512)
  5906.     DECLARE @log_path_quoted_for_copy nvarchar(512)
  5907.  
  5908.     DECLARE @logical_data_file nvarchar(255)
  5909.     DECLARE @logical_log_file nvarchar(255)
  5910.     DECLARE @canneddbdata_file nvarchar(255)
  5911.     DECLARE @canneddblog_file nvarchar(255)
  5912.     DECLARE @filecopy_cmd nvarchar(255)
  5913.     DECLARE @file_exists bit
  5914.     DECLARE @data_file_preexists int
  5915.     DECLARE @log_file_preexists int
  5916.     DECLARE @osql_path nvarchar(260)
  5917.     DECLARE @osql_cmd nvarchar(1000)
  5918.     DECLARE @osql_for_nt int
  5919.     DECLARE @devnum int
  5920.     --DECLARE @num_pages int
  5921.     DECLARE @retcode int
  5922.     DECLARE @reg_key nvarchar(255)
  5923.     DECLARE @agentname nvarchar(100)
  5924.     DECLARE @command nvarchar (2048)
  5925.     DECLARE @distbit int
  5926.     DECLARE @install_path nvarchar(255)
  5927.     DECLARE @mssql_data_path nvarchar(255)
  5928.     DECLARE @on_clause nvarchar(512)
  5929.     DECLARE @logon_clause nvarchar(512)
  5930.     DECLARE @distproc nvarchar(255)
  5931.     DECLARE @major_version int
  5932.     DECLARE @db_exists bit
  5933.     DECLARE @trunc_log_bit int
  5934.     DECLARE @description nvarchar(100)
  5935.     DECLARE @category_name sysname
  5936.     DECLARE @createmode_attach int
  5937.     DECLARE @createmode_noattach int
  5938.     DECLARE @createmode_fordetach int
  5939.  
  5940.     --DECLARE @filegrowth nvarchar(10)
  5941.     DECLARE @data_file_size_str nvarchar(10)
  5942.     DECLARE @log_file_size_str nvarchar(10)
  5943.     DECLARE @platform_nt binary
  5944.  
  5945.     --DECLARE @max_datafile_size int
  5946.     --DECLARE @max_logfile_size int
  5947.     
  5948.     IF @password = N''
  5949.         select @password = NULL
  5950.  
  5951.     select @platform_nt = 0x1
  5952.     --select @filegrowth = N'512KB'
  5953.  
  5954.     -- on error, delete the data and log files only if they didn't pre-exist.
  5955.     -- by default, assume they pre-exist.
  5956.     select @data_file_preexists = 1
  5957.     select @log_file_preexists = 1
  5958.     select @file_exists = 0
  5959.  
  5960.     if (@data_file_size IS NULL) or (@data_file_size = 0)
  5961.         select @data_file_size_str = N'512KB'
  5962.     else
  5963.         select @data_file_size_str = convert(nvarchar(10), @data_file_size)
  5964.  
  5965.     if (@log_file_size IS NULL) or (@log_file_size = 0)
  5966.         select @log_file_size_str = N'512KB'
  5967.     else
  5968.         select @log_file_size_str = convert(nvarchar(10), @log_file_size)
  5969.     
  5970.     --if (@data_file_size > 16)
  5971.     --  select @max_datafile_size = @data_file_size
  5972.     --else
  5973.     --  select @max_datafile_size = 16
  5974.     
  5975.     --if (@log_file_size > 16)
  5976.     --  select @max_logfile_size = @log_file_size
  5977.     --else
  5978.     --  select @max_logfile_size = 16
  5979.  
  5980.     select @createmode_attach = 0, @createmode_noattach = 1, @createmode_fordetach = 2
  5981.     SELECT @trunc_log_bit = 8
  5982.     SELECT @distbit = 16
  5983.  
  5984.     if (@createmode <> @createmode_fordetach)
  5985.     begin
  5986.     
  5987.         /* 
  5988.         ** Check if replication components are installed on this server
  5989.         */
  5990.         exec @retcode = dbo.sp_MS_replication_installed
  5991.         if (@retcode <> 1)
  5992.         begin
  5993.             return (1)
  5994.         end
  5995.     
  5996.         /* 
  5997.         ** Check for invalid security modes
  5998.         */
  5999.         IF @security_mode < 0 OR @security_mode > 1
  6000.         BEGIN
  6001.             RAISERROR(14109, 16, -1)
  6002.             RETURN (1)
  6003.         END
  6004.  
  6005.         IF ( ( @platform_nt != platform() & @platform_nt ) and @security_mode = 1)
  6006.         BEGIN
  6007.             RAISERROR(21038, 16, -1)
  6008.             RETURN (1)
  6009.         END
  6010.     
  6011.         /* 
  6012.         ** Check for invalid retention values 
  6013.         */
  6014.         IF @min_distretention < 0 OR @max_distretention < 0 
  6015.         BEGIN
  6016.             RAISERROR(14106, 16, -1)
  6017.             RETURN (1)
  6018.         END
  6019.         IF @min_distretention > @max_distretention
  6020.         BEGIN
  6021.             RAISERROR(14107, 16, -1) 
  6022.             RETURN (1)
  6023.         END
  6024.  
  6025.         /*
  6026.         ** Check to make sure this is a distributor
  6027.         */
  6028.         IF NOT EXISTS (SELECT * FROM master..sysservers
  6029.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  6030.                  AND srvstatus & 8 <> 0)
  6031.         BEGIN
  6032.             RAISERROR (14114, 16, -1, @@SERVERNAME)
  6033.             RETURN(1)
  6034.         END
  6035.     
  6036.         /*
  6037.         ** Check if database is already configured as a distributor database
  6038.         */
  6039.         IF EXISTS (SELECT * FROM msdb..MSdistributiondbs WHERE name = @database collate database_default)
  6040.         BEGIN
  6041.             RAISERROR (14119, 16, -1, @database)
  6042.             RETURN(1)
  6043.         END    
  6044.     end
  6045.  
  6046.     /* 
  6047.     ** Get path to osql client (TOOLS) directory
  6048.     */
  6049.     EXECUTE @retcode = master.dbo.sp_MSgettools_path @osql_path OUTPUT
  6050.     IF ( @retcode <> 0 ) or ( @@ERROR <> 0 ) or ( @osql_path is NULL ) or ( @osql_path = '' )
  6051.     BEGIN
  6052.         GOTO UNDO       
  6053.     END
  6054.  
  6055.     /* 
  6056.     ** Get path to version specific INSTALL directory
  6057.     */
  6058.     exec @retcode = master.dbo.sp_MSget_setup_paths
  6059.         @sql_path = @install_path output,
  6060.         @data_path = @mssql_data_path output
  6061.     IF @retcode <> 0 or @install_path is NULL or @install_path='' or @mssql_data_path = ''
  6062.     BEGIN
  6063.         GOTO UNDO       
  6064.     END
  6065.  
  6066.     IF @data_folder IS NULL or @data_folder = ''
  6067.         select @data_folder = @mssql_data_path + '\DATA'
  6068.  
  6069.     IF @log_folder IS NULL or @log_folder = ''
  6070.         select @log_folder = @mssql_data_path + '\DATA'
  6071.  
  6072.     IF @data_file IS NULL
  6073.         SELECT @data_file = @database + '.MDF'
  6074.  
  6075.     IF @log_file IS NULL
  6076.         SELECT @log_file = @database + '.LDF'
  6077.  
  6078.     if substring(@data_folder, len(@data_folder), 1) = '\'
  6079.     select @data_folder = substring (@data_folder, 1, len(@data_folder) -1)
  6080.     if substring(@log_folder, len(@log_folder), 1) = '\'
  6081.     select @log_folder = substring (@log_folder, 1, len(@log_folder) -1)
  6082.  
  6083.     SELECT @data_path = @data_folder + '\' + @data_file
  6084.     SELECT @log_path = @log_folder + '\' + @log_file
  6085.  
  6086.     SELECT @data_path_quoted_for_copy = '"' + @data_folder + '\' + @data_file + '"'
  6087.     SELECT @log_path_quoted_for_copy = '"' + @log_folder + '\' + @log_file + '"'
  6088.  
  6089.     select @logical_data_file = @database
  6090.  
  6091.     /* 
  6092.     ** Truncate the logical log file name back to 128 characters
  6093.     ** long so the 'CREATE DATABASE' statement won't complain.
  6094.     */
  6095.     /* LEN(@logical_log_file) = LEN(@database) + LEN('_log') and
  6096.        LEN(@logical_log_file) <= 128 implies 
  6097.        LEN(@database) <=124 */
  6098.     IF (LEN(@database) > 124)
  6099.         SELECT @logical_log_file = SUBSTRING(@database, 1, 124) + '_log'  
  6100.     ELSE 
  6101.         SELECT @logical_log_file = @database + '_log'
  6102.  
  6103.     if (@createmode = @createmode_attach)
  6104.     begin
  6105.         select @canneddbdata_file = @mssql_data_path + '\DATA\DISTMDL.MDF'
  6106.         select @canneddblog_file = @mssql_data_path + '\DATA\DISTMDL.LDF'
  6107.  
  6108.         exec dbo.sp_MSget_file_existence @canneddbdata_file, @file_exists OUTPUT
  6109.         if (@file_exists = 0)
  6110.         begin
  6111.             /* Fallback to mode where instdist.sql needs to be run */
  6112.             select @createmode = @createmode_noattach
  6113.         end
  6114.  
  6115.         exec dbo.sp_MSget_file_existence @canneddblog_file, @file_exists OUTPUT
  6116.         if (@file_exists = 0)
  6117.         begin
  6118.             /* Fallback to mode where instdist.sql needs to be run */
  6119.             select @createmode = @createmode_noattach
  6120.         end
  6121.     end
  6122.  
  6123.     /*
  6124.     ** Create the distributor database if it does not exist
  6125.     */
  6126.     IF NOT EXISTS (SELECT * from master..sysdatabases WHERE name = @database collate database_default) AND (@createmode <> @createmode_attach)
  6127.     BEGIN
  6128.  
  6129.         -- Note: Use system's default file growth.
  6130.         IF @logical_data_file IS NOT NULL AND NOT EXISTS (SELECT * FROM master..sysdevices WHERE name = @logical_data_file collate database_default)
  6131.         BEGIN
  6132.             SELECT @on_clause = ' ON (NAME =''' + @logical_data_file + ''',FILENAME=''' + REPLACE( @data_path, '''', '''''' ) + 
  6133.                 ''', SIZE=' + @data_file_size_str + ', MAXSIZE = UNLIMITED)'
  6134.         END
  6135.  
  6136.         IF @logical_log_file IS NOT NULL AND NOT EXISTS (SELECT * FROM master..sysdevices WHERE name = @logical_log_file collate database_default)
  6137.         BEGIN
  6138.             SELECT @logon_clause = ' LOG ON (NAME =''' + @logical_log_file + ''',FILENAME=''' + REPLACE( @log_path, '''', '''''' ) + 
  6139.                 ''', SIZE=' + @log_file_size_str + ', MAXSIZE= UNLIMITED)'          
  6140.         END
  6141.  
  6142.         /*
  6143.         ** Create distributor database
  6144.         */
  6145.         SELECT @command = 'USE master  CREATE DATABASE ' +  QUOTENAME(@database) + 
  6146.             + isnull(@on_clause, ' ') + isnull(@logon_clause, ' ')
  6147.  
  6148.         EXEC (@command)
  6149.         IF @@ERROR <> 0
  6150.             RETURN (1)
  6151.         SELECT @db_exists = 0
  6152.     END
  6153.     ELSE IF NOT EXISTS (SELECT * from master..sysdatabases WHERE name = @database collate database_default) AND (@createmode = @createmode_attach)
  6154.     BEGIN
  6155.     /* DO THE CREATE DATABASE FOR ATTACH STUFF */
  6156.         
  6157.         exec dbo.sp_MSget_file_existence @data_path, @data_file_preexists OUTPUT
  6158.         if (@data_file_preexists = 1)
  6159.         begin
  6160.             raiserror(5170, 16, -1, @data_path)
  6161.             return 1
  6162.         end
  6163.     
  6164.         SELECT @on_clause = ' ON (NAME = ''' + @logical_data_file + ''', FILENAME=''' + REPLACE( @data_path, '''', '''''' ) + ''')'
  6165.         
  6166.         exec dbo.sp_MSget_file_existence @log_path, @log_file_preexists OUTPUT
  6167.         if (@log_file_preexists = 1)
  6168.         begin
  6169.             raiserror(5170, 16, -1, @log_path)
  6170.             return 1
  6171.         end
  6172.  
  6173.         SELECT @logon_clause = ' LOG ON (NAME = ''' + @logical_log_file + ''', FILENAME=''' + REPLACE( @log_path, '''', '''''' ) + ''')'
  6174.  
  6175.         select @filecopy_cmd = 'copy "' + @canneddbdata_file + '" ' + @data_path_quoted_for_copy
  6176.         EXEC @retcode = master..xp_cmdshell @filecopy_cmd, NO_OUTPUT
  6177.         IF @retcode <> 0 OR @@ERROR <> 0
  6178.         BEGIN
  6179.             RAISERROR (14113, 16, -1, @filecopy_cmd, 'instdist.out')
  6180.             return (1)
  6181.         END
  6182.  
  6183.         select @filecopy_cmd = 'copy "' + @canneddblog_file + '" ' + @log_path_quoted_for_copy
  6184.         EXEC @retcode = master..xp_cmdshell @filecopy_cmd, NO_OUTPUT
  6185.         IF @retcode <> 0 OR @@ERROR <> 0
  6186.         BEGIN
  6187.             RAISERROR (14113, 16, -1, @filecopy_cmd, 'instdist.out')
  6188.             return (1)
  6189.         END
  6190.  
  6191.         /*
  6192.         ** Create distributor database
  6193.         */
  6194.         SELECT @command = 'USE master  CREATE DATABASE ' +  QUOTENAME(@database) + 
  6195.             + @on_clause + @logon_clause + ' FOR ATTACH'
  6196.  
  6197.         EXEC (@command)
  6198.         IF @@ERROR <> 0
  6199.         begin
  6200.             RETURN (1)
  6201.         end
  6202.         dbcc dbreindexall(@database, 240) with no_infomsgs
  6203.  
  6204.         SELECT @db_exists = 0
  6205.     END
  6206.     ELSE
  6207.     BEGIN
  6208.         SELECT @db_exists = 1
  6209.     END
  6210.  
  6211.     -- Must make the dist db owned by sa so that the sps in it can select from
  6212.     -- security cache tables in tempdb by owership chain rule.
  6213.     declare @retcode2 int
  6214.     select @retcode2 = 0
  6215.     select @distproc = QUOTENAME(@database) + '.dbo.sp_executesql'
  6216.     SELECT @command = 
  6217.         -- If the db is created by sa or from attach, sa is dbo already.
  6218.         -- sp_changedbowner will fail is the new owner is an user in the db already.
  6219.         ' if not exists (select * from sysusers where sid = 0x01) ' +
  6220.         ' exec @retcode2 = dbo.sp_changedbowner ''sa'''
  6221.     EXEC @retcode = @distproc @command, N'@retcode2 int output', @retcode2 output
  6222.     IF @retcode <> 0 or @retcode2 <> 0 or @@ERROR <> 0
  6223.     BEGIN
  6224.         GOTO UNDO
  6225.     END
  6226.  
  6227.     /* Set the database option truncate log on checkpoint & turn off autoclose which is default of win9x*/
  6228.     IF EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE 
  6229.         name = @database collate database_default AND
  6230.         (status & @trunc_log_bit) = 0 )   -- if its not already marked
  6231.     BEGIN
  6232.         EXEC @retcode = dbo.sp_dboption @database, 'trunc. log on chkpt.', 'true'
  6233.         IF @retcode <> 0 OR @@ERROR <> 0
  6234.         BEGIN
  6235.             GOTO UNDO
  6236.         END
  6237.     END
  6238.  
  6239.     EXEC @retcode = dbo.sp_dboption @database, 'autoclose', 'false'
  6240.         IF @retcode <> 0 OR @@ERROR <> 0
  6241.         BEGIN
  6242.             GOTO UNDO
  6243.         END
  6244.  
  6245.     /*
  6246.     **
  6247.     ** Update sysdatabase category bit
  6248.     ** This is to prevent user from dropping the database.
  6249.     **/
  6250.     if (@createmode <> @createmode_fordetach)
  6251.     begin
  6252.         UPDATE master..sysdatabases SET category = category | @distbit WHERE name = @database collate database_default
  6253.         IF @@ERROR <> 0
  6254.         BEGIN
  6255.             GOTO UNDO
  6256.         END
  6257.     end
  6258.  
  6259.     /* 
  6260.     ** Install instdist.sql
  6261.     */
  6262.  
  6263.     if (@createmode <> @createmode_attach) OR (@db_exists = 1)
  6264.     begin
  6265.         if (( platform() & @platform_nt = @platform_nt ))
  6266.             select @osql_for_nt = 1
  6267.         else
  6268.             select @osql_for_nt = 0
  6269.  
  6270.         -- Always use integrated security on WINNT since @login passed-in is for remote 
  6271.         -- subscriber and may not have enough privilege to apply the script
  6272.         IF (@security_mode = 1 or @osql_for_nt = 1) AND NOT (@security_mode = 0 AND @createmode = 2)
  6273.         BEGIN
  6274.             SELECT @osql_cmd = '" "' + @osql_path + '\binn\osql" -E '  
  6275.             if serverproperty('instancename') is not null
  6276.                 SELECT @osql_cmd = @osql_cmd + ' -S"' + @@SERVERNAME + '" '
  6277.         END
  6278.         ELSE
  6279.         BEGIN
  6280.         -- cannot specify -S w/ -E for local execution, SID does not map 
  6281.         if (@osql_for_nt = 1)
  6282.             SELECT @osql_cmd = '" "' + @osql_path + '\binn\osql" -U"' + @login + '" -P"' + 
  6283.                 isnull(@password,'') + '" -S"' + @@SERVERNAME + '" '
  6284.         else
  6285.             SELECT @osql_cmd = '"'   + @osql_path + '\binn\osql" -U"' + @login + '" -P"' + 
  6286.                 isnull(@password,'') + '" -S"' + @@SERVERNAME + '" '
  6287.         END
  6288.     
  6289.         select @osql_cmd = @osql_cmd + '-l60 -t60 '
  6290.  
  6291.         -- We must use -b option to make osql return error code !!
  6292.         SELECT @osql_cmd = @osql_cmd + 
  6293.             ' -d"' + @database + '" -b ' +
  6294.             ' -i' + '"' + @install_path + '\install\instdist.sql"' + 
  6295.             ' -o' + '"' + @install_path + '\install\instdist.out"'
  6296.  
  6297.         if (@osql_for_nt = 1)
  6298.         BEGIN
  6299.             SELECT @osql_cmd = @osql_cmd + ' "'
  6300.         END
  6301.     
  6302.         EXEC @retcode = master..xp_cmdshell @osql_cmd
  6303.         IF @retcode <> 0 OR @@ERROR <> 0
  6304.         BEGIN
  6305.             RAISERROR (14113, 16, -1, @osql_cmd, 'instdist.out')
  6306.             GOTO UNDO       
  6307.         END
  6308.     end
  6309.     
  6310.     if (@createmode <> @createmode_fordetach)
  6311.     begin
  6312.         /* Set db_existed bit in MSrepl_version */
  6313.         IF @db_exists = 1
  6314.         BEGIN
  6315.             SELECT @distproc = 'UPDATE ' +
  6316.                 @database + '..MSrepl_version SET db_existed = 0x1'
  6317.     
  6318.             EXEC(@distproc)
  6319.             IF @@ERROR <> 0
  6320.             BEGIN
  6321.                 GOTO UNDO       
  6322.             END
  6323.         END
  6324.  
  6325.         DELETE msdb.dbo.MSdistributiondbs WHERE name = @database collate database_default
  6326.         IF @@ERROR <> 0
  6327.         BEGIN
  6328.             GOTO UNDO       
  6329.         END
  6330.  
  6331.         INSERT INTO msdb.dbo.MSdistributiondbs VALUES (
  6332.             @database, @min_distretention, @max_distretention, @history_retention
  6333.             )
  6334.         IF @@ERROR <> 0
  6335.         BEGIN
  6336.             GOTO UNDO       
  6337.         END
  6338.  
  6339.         -- This login need db_owner priviledge to call sps in distribution db
  6340.         declare @distributor_login sysname
  6341.         select @distributor_login = 'distributor_admin'
  6342.  
  6343.         select @command = quotename(@database) + '.dbo.sp_MSrepl_dbrole'
  6344.         exec @retcode = @command 'db_owner', @distributor_login, 'add'
  6345.         IF @@error <> 0 OR @retcode <> 0
  6346.             GOTO UNDO
  6347.  
  6348.         if @from_scripting = 0
  6349.         begin
  6350.             /*
  6351.             ** Create the history cleanup agent.
  6352.             */
  6353.             SELECT @agentname = formatmessage (20567, @database)
  6354.             SELECT @command =  'EXEC dbo.sp_MShistory_cleanup @history_retention = ' + 
  6355.                 CONVERT(nvarchar(12), @history_retention)
  6356.  
  6357.             IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @agentname collate database_default and
  6358.                 UPPER(originating_server) = UPPER(CONVERT(sysname, SERVERPROPERTY('ServerName'))))
  6359.             BEGIN
  6360.                 EXEC @retcode = msdb.dbo.sp_delete_job 
  6361.                     @job_name = @agentname
  6362.                 IF @@ERROR <> 0 or @retcode <> 0
  6363.                 BEGIN
  6364.                     GOTO UNDO
  6365.                 END
  6366.             END
  6367.  
  6368.             set @description = formatmessage(20535)
  6369.  
  6370.             -- Get History Cleanup category name (assumes category_id = 12)
  6371.             select @category_name = name FROM msdb.dbo.syscategories where category_id = 12
  6372.  
  6373.             EXECUTE @retcode = dbo.sp_MSadd_repl_job @agentname,
  6374.             @subsystem = 'TSQL',
  6375.             @server = @@SERVERNAME,
  6376.             @databasename = @database,
  6377.             @description = @description,
  6378.             @freqtype = 4,    
  6379.             @freqsubtype = 4,         
  6380.             @freqsubinterval = 10,    /* Number of minutes between runs */ 
  6381.             @command = @command,
  6382.             @enabled = 1,
  6383.             @retryattempts = 0,
  6384.             @loghistcompletionlevel = 0,
  6385.             @category_name = @category_name
  6386.     
  6387.             IF @@ERROR <> 0 or @retcode <> 0
  6388.             BEGIN
  6389.                 GOTO UNDO
  6390.             END
  6391.  
  6392.             /*
  6393.             ** Create the distribution cleanup agent.
  6394.             */
  6395.             SELECT @agentname = formatmessage (20568, @database)
  6396.             SELECT @command =  'EXEC dbo.sp_MSdistribution_cleanup @min_distretention = ' + 
  6397.                 CONVERT(nvarchar(12), @min_distretention) + ', @max_distretention = ' +
  6398.                 CONVERT(nvarchar(12), @max_distretention)
  6399.  
  6400.             IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @agentname collate database_default and
  6401.                 UPPER(originating_server) = UPPER(CONVERT(sysname, SERVERPROPERTY('ServerName'))))
  6402.             BEGIN
  6403.                 EXEC @retcode = msdb.dbo.sp_delete_job 
  6404.                     @job_name = @agentname
  6405.                 IF @@ERROR <> 0 or @retcode <> 0
  6406.                 BEGIN
  6407.                     GOTO UNDO
  6408.                 END
  6409.             END
  6410.  
  6411.             set @description = formatmessage(20541)
  6412.             -- Get Distribution Cleanup category name (assumes category_id = 11)
  6413.             select @category_name = name FROM msdb.dbo.syscategories where category_id = 11
  6414.     
  6415.             EXECUTE @retcode = msdb.dbo.sp_MSadd_repl_job @agentname,
  6416.             @subsystem = 'TSQL',
  6417.             @server = @@SERVERNAME,
  6418.             @databasename = @database,
  6419.             @description = @description,
  6420.             @freqtype = 4,    
  6421.             @freqsubtype = 4,         
  6422.             @freqsubinterval = 10,    /* Number of minutes between runs */ 
  6423.             @command = @command,
  6424.             @retryattempts = 0,
  6425.             @enabled = 0,
  6426.             @loghistcompletionlevel = 0,
  6427.             @category_name = @category_name,
  6428.             -- Start  and end time is 5 min off from the history cleanup, which use the default.
  6429.             @activestarttimeofday = 000500,
  6430.             @activeendtimeofday   = 000459
  6431.  
  6432.             IF @@ERROR <> 0 or @retcode <> 0
  6433.             BEGIN
  6434.                 GOTO UNDO
  6435.             end
  6436.         end
  6437.     end
  6438.     else
  6439.     begin
  6440.         /*detach */
  6441.         dbcc detachdb(@database)
  6442.     end
  6443.     
  6444.     RETURN(0)
  6445.  
  6446. UNDO:
  6447.  
  6448.     IF @db_exists = 0
  6449.         EXECUTE dbo.sp_dropdistributiondb @database
  6450.  
  6451.     /* Need to do it since sp_dropdistributiondb will fail in some cases */
  6452.     UPDATE master..sysdatabases SET category = category & ~@distbit WHERE name = @database collate database_default
  6453.     
  6454.     DELETE msdb.dbo.MSdistributiondbs where name = @database collate database_default
  6455.  
  6456.     /* drop the database and ignore error */
  6457.     IF @db_exists = 0 AND
  6458.         EXISTS (SELECT * from master..sysdatabases WHERE name = @database collate database_default)
  6459.     BEGIN
  6460.         SELECT @command = 'USE master  DROP DATABASE ' +  QUOTENAME(@database) 
  6461.         EXEC (@command)
  6462.     END
  6463.  
  6464.     if (@createmode = @createmode_attach)
  6465.     begin
  6466.         if (@data_file_preexists = 0)
  6467.         begin
  6468.             select @command = 'del ' + @data_path_quoted_for_copy
  6469.             exec master..xp_cmdshell @command
  6470.             --ignore errors
  6471.         end
  6472.         if (@log_file_preexists = 0)
  6473.         begin
  6474.             select @command = 'del ' + @log_path_quoted_for_copy
  6475.             exec master..xp_cmdshell @command
  6476.             --ignore errors
  6477.         end
  6478.     end
  6479.         
  6480.     RETURN(1)        
  6481. GO
  6482.  
  6483. dump tran master with no_log
  6484. GO
  6485.  
  6486. raiserror('Creating procedure sp_dropdistpublisher', 0,1)
  6487. go
  6488.  
  6489. CREATE PROCEDURE sp_dropdistpublisher (
  6490.     @publisher sysname,         /* publisher server name */
  6491.     @no_checks bit = 0
  6492.         ) AS
  6493.  
  6494.     SET NOCOUNT ON
  6495.  
  6496.     DECLARE @distributor sysname
  6497.     DECLARE @distaccount nvarchar(127)
  6498.     DECLARE @proc nvarchar (255)
  6499.     DECLARE @retcode int
  6500.     DECLARE @privilege sysname
  6501.     DECLARE @reg_key nvarchar(255)
  6502.     DECLARE @return_status int
  6503.     DECLARE @foundSubscriber int 
  6504.     DECLARE @distribdb sysname
  6505.     DECLARE @command nvarchar(255)
  6506.     DECLARE @active_value int
  6507.     DECLARE @publish_bit int
  6508.     DECLARE @mergepub_bit int
  6509.  
  6510.  
  6511.     SELECT @return_status = 0
  6512.     SELECT @foundSubscriber = 0
  6513.  
  6514.     SELECT @publish_bit = 1
  6515.     SELECT @mergepub_bit = 4
  6516.  
  6517.     /*
  6518.     ** Parameter Check:  @publisher.
  6519.     ** Check to make sure that the publisher exists, that the name isn't
  6520.     ** NULL, and that the name conforms to the rules for identifiers.
  6521.     */
  6522.  
  6523.     IF @publisher IS NULL
  6524.         BEGIN
  6525.             RAISERROR (14043, 16, -1, '@publisher')
  6526.             RETURN (1)
  6527.         END
  6528.  
  6529.     EXECUTE @retcode = dbo.sp_validname @publisher
  6530.  
  6531.     IF @retcode <> 0
  6532.         RETURN (1)
  6533.  
  6534.     /*
  6535.     ** Get distribution server information for remote RPC
  6536.     ** agent verification.
  6537.     */
  6538.     EXEC @retcode = dbo.sp_helpdistributor @publisher = @publisher,
  6539.         @distributor = @distributor OUTPUT,
  6540.         @distribdb = @distribdb OUTPUT
  6541.     IF @@error <> 0 OR @retcode <> 0
  6542.         BEGIN
  6543.         RAISERROR (14071, 16, -1)
  6544.            RETURN (1)
  6545.     END
  6546.  
  6547.     /*
  6548.     ** Only a local distributor can be modified.
  6549.     */
  6550.     IF UPPER(@distributor) <> UPPER(@@SERVERNAME)
  6551.         BEGIN
  6552.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  6553.         RETURN (1)
  6554.         END
  6555.  
  6556.     IF NOT EXISTS (SELECT *
  6557.     FROM msdb..MSdistpublishers
  6558.             WHERE UPPER(name) = UPPER(@publisher) collate database_default)
  6559.     BEGIN
  6560.         RAISERROR (14080, 11, -1)
  6561.         RETURN (1)
  6562.     END
  6563.  
  6564.  
  6565.     IF UPPER(@publisher) = UPPER(@@SERVERNAME)
  6566.     BEGIN
  6567.         /* 
  6568.         ** If @publisher is local, 
  6569.         ** 1. check to make sure there is no subscriber 
  6570.         ** for the publisher in the distribution database
  6571.         ** 2. check to make sure there are not database enabled for replication
  6572.         */
  6573.         -- Check to see if there are subscribers defined.
  6574.         if exists (SELECT *
  6575.                      FROM master..sysservers
  6576.                     WHERE srvstatus & 4 <> 0)
  6577.         begin
  6578.             raiserror(21047, 16, -1)
  6579.             return(1)
  6580.         end
  6581.         
  6582.         IF EXISTS (SELECT * FROM master..sysdatabases WHERE 
  6583.             (category & @publish_bit) <> 0 OR
  6584.             (category & @mergepub_bit) <> 0)
  6585.         BEGIN 
  6586.             RAISERROR (21033, 16, -1, @@SERVERNAME)
  6587.             RETURN (1)
  6588.         END
  6589.     END
  6590.     ELSE
  6591.     BEGIN
  6592.         if @no_checks = 0
  6593.         begin
  6594.             /* 
  6595.             ** If the publisher is remote, check the status of the distpublisher 
  6596.             ** The status will be inactive if the remote publisher dropped the
  6597.             ** distributor.
  6598.             */
  6599.  
  6600.             IF EXISTS (SELECT * FROM msdb.dbo.MSdistpublishers WHERE
  6601.                 UPPER(name) = UPPER(@publisher) collate database_default and active = 1)
  6602.             BEGIN
  6603.                 RAISERROR (14098, 16, -1, @publisher, @@SERVERNAME)
  6604.                 RETURN (1)
  6605.             END
  6606.         end
  6607.         else
  6608.         begin
  6609.             SELECT @command = @distribdb + '.dbo.sp_MSdistpublisher_cleanup'
  6610.             exec @retcode = @command @publisher
  6611.             if @retcode <> 0 or @@error <> 0
  6612.                 return 1
  6613.         end
  6614.     END
  6615.  
  6616.     DELETE msdb..MSdistpublishers where UPPER(name) = UPPER(@publisher) collate database_default
  6617.     IF @@ERROR <> 0  RETURN (1)
  6618.  
  6619.  
  6620.     declare @fExists int
  6621.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 'sa'
  6622.     if (@fExists = 1)
  6623.     BEGIN
  6624.        EXECUTE @retcode = dbo.sp_dropremotelogin @publisher, 'distributor_admin', sa
  6625.        IF @@ERROR <> 0 OR @retcode <> 0 RETURN (1)
  6626.     END
  6627.  
  6628.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 
  6629.         'distributor_admin'
  6630.     if (@fExists = 1)
  6631.     BEGIN
  6632.        EXECUTE @retcode = dbo.sp_dropremotelogin @publisher, 'distributor_admin',
  6633.             'distributor_admin'
  6634.        IF @@ERROR <> 0 OR @retcode <> 0 RETURN (1)
  6635.     END
  6636.  
  6637. /* SECURITY ********************************
  6638.     IF EXISTS (SELECT * FROM master..sysremotelogins
  6639.        WHERE remoteserverid = (SELECT srvid FROM master..sysservers
  6640.        WHERE UPPER(srvname) = UPPER(@publisher) collate database_default)
  6641.        AND remoteusername = 'sa'
  6642.        AND suid = 1)     --'sa' 
  6643. *********************************/
  6644.  
  6645.  
  6646. /* SECURITY ********************************
  6647.     IF EXISTS (SELECT * FROM master..sysremotelogins
  6648.        WHERE remoteserverid = (SELECT srvid FROM master..sysservers
  6649.        WHERE UPPER(srvname) = UPPER(@publisher) collate databsae_default)
  6650.        AND remoteusername = 'probe'
  6651.        AND suid = 10)     -- 'probe' 
  6652.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'probe', 'probe'
  6653.     if (@fExists = 1)
  6654.     BEGIN
  6655.        EXECUTE @retcode = dbo.sp_remoteoption @publisher, probe, probe, trusted, false
  6656.        IF @@ERROR <> 0 OR @retcode <> 0 RETURN (1)
  6657.     END
  6658. *********************************/
  6659.  
  6660.     return(@return_status)
  6661. GO
  6662.  
  6663. raiserror('Creating procedure sp_adddistpublisher', 0,1)
  6664. go
  6665.  
  6666. CREATE PROCEDURE sp_adddistpublisher (
  6667.     @publisher sysname,      /* publisher server name */
  6668.     @distribution_db sysname,
  6669.     @security_mode int = NULL,
  6670.     @login sysname = 'sa',
  6671.     @password sysname = NULL,
  6672.     @working_directory nvarchar(255),
  6673.     @trusted nvarchar(5) = NULL,
  6674.     @encrypted_password bit = 0,
  6675.     @thirdparty_flag bit = 0
  6676.         ) AS
  6677.  
  6678.     SET NOCOUNT ON
  6679.  
  6680.     /*
  6681.     ** Declarations.
  6682.     */
  6683.  
  6684.     DECLARE @retcode int
  6685.     DECLARE @reg_key nvarchar(255)
  6686.     DECLARE @distbit int
  6687.     DECLARE @active_value int
  6688.     DECLARE @server_added bit
  6689.     DECLARE @proc nvarchar(255)
  6690.     declare @fExists int
  6691.     declare @command nvarchar(255)
  6692.     declare @trusted_id bit
  6693.     declare @platform_nt binary
  6694.     declare @qv_replication varchar(10)
  6695.     declare @qv_replication_unlimited integer
  6696.     declare @qv_value_replication integer
  6697.     declare @enc_password nvarchar(524)
  6698.  
  6699.     select @platform_nt = 0x1
  6700.     select @qv_replication = '2745196162', @qv_replication_unlimited = 0
  6701.  
  6702.     SELECT @distbit = 16
  6703.     SELECT @server_added = 0
  6704.  
  6705.     /* 
  6706.     ** Check if replication components are installed on this server
  6707.     */
  6708.     exec @retcode = dbo.sp_MS_replication_installed
  6709.     if (@retcode <> 1)
  6710.     begin
  6711.         return (1)
  6712.     end
  6713.  
  6714.     IF @working_directory IS NULL or ltrim(rtrim(@working_directory)) = ' '
  6715.     BEGIN
  6716.         RAISERROR (14043, 16, -1, '@working_directory')
  6717.         return (1)
  6718.     END
  6719.  
  6720.  
  6721.     /*
  6722.     ** Parameter Check:  @publisher.
  6723.     ** Check to make sure that the publisher is not NULL and that it
  6724.     ** conforms to the rules for identifiers.
  6725.     */
  6726.  
  6727.     IF @publisher IS NULL
  6728.         BEGIN
  6729.             RAISERROR (14043, 16, -1, '@publisher')
  6730.             return (1)
  6731.         END
  6732.  
  6733.     EXECUTE @retcode = dbo.sp_validname @publisher
  6734.  
  6735.     IF @@ERROR <> 0 OR @retcode <> 0
  6736.         return (1)
  6737.  
  6738.     IF @password = N''
  6739.         select @password = NULL
  6740.  
  6741.     /* On REPLICATION_LIMITED server, only local publisher is supported.
  6742.      * Note: The login and password registered for local publisher will be used for 
  6743.      * local agents to login to distributor, thus local publisher has to be installed first.
  6744.      * We choose not to support remote dist publshers on REPLICATION_LIMITED server altogether.
  6745.      * On NT, local agents will always use integrated security to log into 
  6746.      * distributor
  6747.      * Today, REPLICATION_LIMITED means desktop but we check specific sku entry just in case
  6748.     */
  6749.     exec @qv_value_replication = master.dbo.sp_MSinstance_qv @qv_replication    
  6750.  
  6751.     if ( @qv_value_replication != @qv_replication_unlimited ) and ( UPPER(@publisher) <> UPPER(@@servername) )
  6752.     begin
  6753.         -- remote dist publisher is not supported on this server version
  6754.         raiserror(21041,16,-1)
  6755.         return (1)
  6756.     end
  6757.  
  6758.  
  6759.     -- Set default security
  6760.     IF @security_mode IS NULL
  6761.     BEGIN
  6762.         IF (UPPER(@publisher) = UPPER(@@SERVERNAME) and ( platform() & @platform_nt = @platform_nt ) )
  6763.             SELECT @security_mode = 1
  6764.         ELSE
  6765.             SELECT @security_mode = 0
  6766.     END
  6767.  
  6768.     /* 
  6769.     ** Check for invalid security mode
  6770.     */
  6771.     IF @security_mode < 0 OR @security_mode > 1
  6772.         BEGIN
  6773.             RAISERROR(14109, 16, -1)
  6774.             return (1)
  6775.         END
  6776.  
  6777.     IF (UPPER(@publisher) = UPPER(@@SERVERNAME) and ( @platform_nt != platform() & @platform_nt ) and @security_mode = 1)
  6778.     BEGIN
  6779.         RAISERROR(21038, 16, -1)
  6780.         RETURN (1)
  6781.     END
  6782.  
  6783.     -- Encrypt the password
  6784.     select @enc_password = @password
  6785.     IF @encrypted_password = 0
  6786.     BEGIN
  6787.         EXEC @retcode = master.dbo.xp_repl_encrypt @enc_password OUTPUT
  6788.         IF @@error <> 0 OR @retcode <> 0
  6789.             return 1
  6790.     END
  6791.  
  6792.     -- Validate the working directory 
  6793.     -- Remove heading and trailing spaces
  6794.     select @working_directory = RTRIM(LTRIM(@working_directory))
  6795.     
  6796.     -- if the last char is '\', remove it.
  6797.     if substring(@working_directory, len(@working_directory),1) = '\'
  6798.         select @working_directory = substring(@working_directory, 1,
  6799.             len(@working_directory)-1)
  6800.  
  6801.     -- Don't do validation if it is a UNC path due to security problem.
  6802.     -- If the server is started as a service using local system account, we
  6803.     -- don't have access to the UNC path.
  6804.     if substring(@working_directory, 1,2) <> '\\'
  6805.     begin
  6806.         select @command = 'dir "' + @working_directory + '"'
  6807.         exec @retcode = master..xp_cmdshell @command, 'no_output'
  6808.         if @@error <> 0
  6809.             return (1)
  6810.         if @retcode <> 0 
  6811.         begin
  6812.             raiserror (21037, 16, -1, @working_directory)
  6813.             return (1)
  6814.         end
  6815.     end
  6816.  
  6817.     /*
  6818.     ** Parameter Check:  @trusted
  6819.     */
  6820.  
  6821.     if @trusted is null
  6822.     begin
  6823.         if UPPER(@publisher) = UPPER(@@servername)
  6824.             select @trusted = 'false'
  6825.         else
  6826.             select @trusted = 'true'
  6827.     end
  6828.  
  6829.     IF LOWER(@trusted collate SQL_Latin1_General_CP1_CS_AS) NOT IN ('true', 'false')
  6830.         BEGIN
  6831.             RAISERROR (14148, 16, -1, '@trusted')
  6832.             RETURN (1)
  6833.         END
  6834.  
  6835.     IF LOWER(@trusted collate SQL_Latin1_General_CP1_CS_AS) = 'true' SELECT @trusted_id = 1
  6836.     ELSE SELECT @trusted_id = 0
  6837.  
  6838.     /*
  6839.     ** Check to make sure this is a distributor
  6840.     */
  6841.     IF NOT EXISTS (SELECT * FROM master..sysservers
  6842.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  6843.                  AND srvstatus & 8 <> 0)
  6844.     BEGIN
  6845.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  6846.         return (1)
  6847.     END
  6848.  
  6849.     /*
  6850.     ** Check if database is configured as a distributor database
  6851.     */
  6852.     IF NOT EXISTS (SELECT * FROM msdb..MSdistributiondbs WHERE name = @distribution_db collate database_default)
  6853.     BEGIN
  6854.         RAISERROR (14117, 16, -1, @distribution_db)
  6855.         return (1)
  6856.     END
  6857.  
  6858.     /* Check if publisher is already defined. */
  6859.     IF EXISTS (SELECT *
  6860.          FROM msdb..MSdistpublishers
  6861.         WHERE UPPER(name) = UPPER(@publisher) collate database_default)
  6862.  
  6863.     BEGIN
  6864.         RAISERROR (14074, 16, -1, @publisher)
  6865.         RETURN (1)
  6866.     END
  6867.  
  6868.  
  6869.  
  6870.     IF NOT EXISTS (SELECT *
  6871.              FROM master..sysservers
  6872.             WHERE UPPER(srvname) = UPPER(@publisher) collate database_default)
  6873.  
  6874.     /* Add the server if it does not exist. */
  6875.     BEGIN
  6876.         EXECUTE @retcode = dbo.sp_addserver @publisher
  6877.         IF @@error <> 0 OR @retcode <> 0
  6878.         BEGIN
  6879.             RAISERROR (14075, 16, -1)
  6880.             GOTO UNDO
  6881.         END
  6882.         SELECT @server_added = 1
  6883.     END
  6884.     ELSE
  6885.     BEGIN
  6886.         SELECT @publisher = fn_getpersistedservernamecasevariation(@publisher) collate database_default
  6887.     END
  6888.      
  6889.     /*
  6890.     ** Set the Active  value.
  6891.     ** If the @publisher is local, set it to true.
  6892.     ** Otherwise, set it to false
  6893.     */
  6894.     IF UPPER(@publisher) = UPPER(@@SERVERNAME)
  6895.         SELECT @active_value = 1
  6896.     ELSE
  6897.         SELECT @active_value = 0
  6898.  
  6899.  
  6900.     DELETE msdb.dbo.MSdistpublishers where UPPER(name) = UPPER(@publisher) collate database_default
  6901.     IF @@ERROR <> 0
  6902.     BEGIN
  6903.         GOTO UNDO       
  6904.     END
  6905.  
  6906.     INSERT INTO msdb.dbo.MSdistpublishers VALUES (
  6907.         @publisher, @distribution_db, @working_directory,
  6908.         @security_mode, @login, @enc_password, @active_value, @trusted_id, @thirdparty_flag)
  6909.     IF @@ERROR <> 0
  6910.     BEGIN
  6911.         GOTO UNDO       
  6912.     END
  6913.  
  6914.     -- Add distributor_admin to distribution_admin non trusted mapping
  6915.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, null, 
  6916.         'distributor_admin'
  6917.     if( @fExists = 0 )
  6918.     BEGIN
  6919.  
  6920.         EXECUTE @retcode = dbo.sp_addremotelogin @publisher, 'distributor_admin', 
  6921.             'distributor_admin'
  6922.         IF @@error <> 0 OR @retcode <> 0
  6923.         BEGIN
  6924.             RAISERROR (14075, 16, -1)
  6925.             GOTO UNDO
  6926.         END
  6927.     END
  6928.  
  6929.     -- For 6x publisher, we still need the trusted sa to sa.
  6930.     -- For 6x publisher upgrading to 7.0, distributor_admin to distributor_admin need to be trusted.
  6931.     -- add remotelogin of SA if it doesn't already exist
  6932.     -- If there's a mapping for remote login sa already, we cannot map it to distributor_admin
  6933.     -- this is the case of server upgraded from 6.5.
  6934.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, null, 'sa'
  6935.     if( @fExists = 0 )
  6936.     BEGIN
  6937.         EXECUTE @retcode = dbo.sp_addremotelogin @publisher, 'distributor_admin', 'sa'
  6938.         IF @@error <> 0 OR @retcode <> 0
  6939.         BEGIN
  6940.             RAISERROR (14075, 16, -1)
  6941.             GOTO UNDO
  6942.         END
  6943.     END
  6944.     if @trusted_id = 1
  6945.     begin
  6946.         exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 'sa'
  6947.         if( @fExists = 1 )
  6948.         BEGIN
  6949.             EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 'sa', trusted, true
  6950.             IF @@error <> 0 OR @retcode <> 0
  6951.             BEGIN
  6952.                 RAISERROR (14075, 16, -1)
  6953.                 GOTO UNDO
  6954.             END
  6955.         END
  6956.  
  6957.         EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 'distributor_admin', trusted, true
  6958.         IF @@error <> 0 OR @retcode <> 0
  6959.         BEGIN
  6960.             RAISERROR (14075, 16, -1)
  6961.             GOTO UNDO
  6962.         END
  6963.     END
  6964.  
  6965.  
  6966.     /* Add remotelogin enabling the 'probe' of the publisher to
  6967.     ** RPC for distribution counter information.
  6968.     */
  6969. /* SECURITY ********************************
  6970.     IF NOT EXISTS (SELECT *
  6971.               FROM master..sysremotelogins srl,
  6972.                master..sysservers ss
  6973.              WHERE UPPER(ss.srvname) = UPPER(@publisher) collate database_default
  6974.            AND srl.remoteserverid = ss.srvid
  6975.            AND srl.remoteusername = 'probe'
  6976.        AND srl.suid = 10)    -- 'probe' 
  6977.     exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'probe', 'probe'
  6978.     if (@fExists = 0)
  6979.     BEGIN
  6980.        EXECUTE @retcode = dbo.sp_addremotelogin @publisher, 'probe', 'probe'
  6981.        IF @@error <> 0 OR @retcode <> 0
  6982.        BEGIN
  6983.         RAISERROR (14075, 16, -1)
  6984.         GOTO UNDO
  6985.        END
  6986.     END
  6987. *********************************/
  6988.     
  6989.     RETURN(0)
  6990.  
  6991. UNDO:
  6992.     -- If the server is marked, drop it
  6993.     IF EXISTS (SELECT *
  6994.          FROM msdb..MSdistpublishers
  6995.         WHERE UPPER(name) = UPPER(@publisher) collate database_default)
  6996.         EXEC dbo.sp_dropdistpublisher @publisher
  6997.  
  6998.     IF @server_added = 1
  6999.         EXEC dbo.sp_dropserver @publisher
  7000.  
  7001.     RETURN(1)
  7002. GO
  7003.  
  7004.  
  7005. raiserror('Creating procedure sp_changedistpublisher', 0,1)
  7006. go
  7007.  
  7008. CREATE PROCEDURE sp_changedistpublisher (
  7009.     @publisher sysname,
  7010.     @property sysname     = NULL,     /* The property to change */
  7011.     @value nvarchar(255)     = NULL      /* The new property value */
  7012.     ) AS
  7013.  
  7014.     SET NOCOUNT ON
  7015.  
  7016.     /*
  7017.     ** Declarations.
  7018.     */
  7019.     DECLARE @retcode int
  7020.     DECLARE @new_database sysname
  7021.     DECLARE @new_security_mode int
  7022.     DECLARE @new_login sysname
  7023.     DECLARE @new_password nvarchar(524)
  7024.     DECLARE @distbit int
  7025.     DECLARE @new_active int
  7026.     DECLARE @new_trusted bit
  7027.     DECLARE @command nvarchar(255)
  7028.     declare @distribdb sysname
  7029.     DECLARE @platform_nt binary
  7030.  
  7031.     SELECT @platform_nt = 0x1
  7032.     SELECT @distbit = 16
  7033.     
  7034.     /*
  7035.     ** Parameter Check:  @property.
  7036.     ** If the @property parameter is NULL, print the options.
  7037.     */
  7038.  
  7039.     IF @property IS NULL
  7040.         BEGIN
  7041.             CREATE TABLE #tab1 (properties sysname collate database_default not null)
  7042.             INSERT INTO #tab1 VALUES ('distribution_db')
  7043.             INSERT INTO #tab1 VALUES ('working_directory')
  7044.             INSERT INTO #tab1 VALUES ('security_mode')
  7045.             INSERT INTO #tab1 VALUES ('login')
  7046.             INSERT INTO #tab1 VALUES ('password')
  7047.             INSERT INTO #tab1 VALUES ('active')
  7048.             INSERT INTO #tab1 VALUES ('trusted')
  7049.             SELECT * FROM #tab1
  7050.             RETURN (0)
  7051.         END
  7052.  
  7053.     /*
  7054.     ** Parameter Check:  @property.
  7055.     ** Check to make sure that @property is a valid property in
  7056.     ** msdb.dbo.MSdistpublishers.
  7057.     */
  7058.     IF @property IS NULL OR LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) NOT IN 
  7059.         ('distribution_db',
  7060.          'working_directory',
  7061.          'security_mode',
  7062.          'login',
  7063.          'password',
  7064.          'active',
  7065.          'trusted')
  7066.         BEGIN
  7067.             RAISERROR (14115, 16, -1, 
  7068.             '''distribution_db'', ''working_directory'', ''security_mode'', ''login'', ''password'', ''active'', or ''trusted''')
  7069.             RETURN (1)
  7070.         END
  7071.  
  7072.     /*
  7073.     ** Check to make sure this is a distributor
  7074.     */
  7075.     IF NOT EXISTS (SELECT * FROM master..sysservers
  7076.               WHERE UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  7077.                  AND srvstatus & 8 <> 0)
  7078.     BEGIN
  7079.         RAISERROR (14114, 16, -1, @@SERVERNAME)
  7080.         RETURN(1)
  7081.     END
  7082.  
  7083.     -- Get the distribution db name.
  7084.     select @distribdb = distribution_db from msdb..MSdistpublishers where
  7085.         UPPER(name) = UPPER(@publisher) collate database_default
  7086.  
  7087.     /*
  7088.     ** Change the property.
  7089.     */
  7090.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'distribution_db'
  7091.         BEGIN
  7092.             IF @value IS NULL
  7093.                 BEGIN
  7094.                     RAISERROR (14043, 16, -1, '@value') 
  7095.                     RETURN (1)
  7096.                 END
  7097.  
  7098.             IF @value <> @distribdb and 
  7099.                 EXISTS (SELECT * FROM msdb.dbo.MSdistpublishers WHERE
  7100.                 UPPER(name) = UPPER(@publisher) collate database_default and active = 1)
  7101.             BEGIN
  7102.                 RAISERROR (21046, 16, -1)
  7103.                 RETURN (1)
  7104.             END
  7105.  
  7106.             /*
  7107.             ** Check if database is configured as a distributor database
  7108.             */
  7109.             IF NOT EXISTS (SELECT * FROM master..sysdatabases
  7110.               WHERE name = @value collate database_default
  7111.                  AND category & @distbit <> 0)
  7112.                 BEGIN
  7113.                     RAISERROR (14117, 16, -1, @new_database)
  7114.                     RETURN(1)
  7115.                 END
  7116.  
  7117.             UPDATE msdb..MSdistpublishers SET distribution_db = @value
  7118.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7119.             IF @@error <> 0 
  7120.                 BEGIN
  7121.                     RETURN (1)
  7122.                 END
  7123.         END
  7124.    
  7125.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'working_directory'
  7126.         BEGIN
  7127.             IF @value IS NULL
  7128.                 BEGIN
  7129.                     RAISERROR (14043, 16, -1, '@value') 
  7130.                     RETURN (1)
  7131.                 END
  7132.  
  7133.             
  7134.             -- Validate the working directory 
  7135.             -- Remove heading and trailing spaces
  7136.             select @value = RTRIM(LTRIM(@value))
  7137.  
  7138.             -- if the last char is '\', remove it.
  7139.  
  7140.             if substring(@value, len(@value),1) = '\'
  7141.                 select @value = substring(@value, 1,
  7142.                     len(@value)-1)
  7143.  
  7144.             -- Don't do validation if it is a UNC path due to security problem.
  7145.             -- If the server is started as a service using local system account, we
  7146.             -- don't have access to the UNC path.
  7147.             if substring(@value, 1,2) <> '\\'
  7148.             begin
  7149.                 select @command = 'dir ' + @value
  7150.                 exec @retcode = master..xp_cmdshell @command, 'no_output'
  7151.                 if @@error <> 0
  7152.                     return 1
  7153.                 if @retcode <> 0 
  7154.                 begin
  7155.                     raiserror (21037, 16, -1, @value)
  7156.                     return 1
  7157.                 end
  7158.             end
  7159.  
  7160.             UPDATE msdb..MSdistpublishers SET working_directory = @value
  7161.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7162.             IF @@error <> 0 
  7163.                 BEGIN
  7164.                     RETURN (1)
  7165.                 END
  7166.         END
  7167.  
  7168.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'security_mode'
  7169.         BEGIN
  7170.             IF @value IS NULL
  7171.                 BEGIN
  7172.                     RAISERROR (14043, 16, -1, '@value') 
  7173.                     RETURN (1)
  7174.                 END
  7175.  
  7176.             /*
  7177.             ** Set the SecurityMode registry key value
  7178.             */
  7179.             SELECT @new_security_mode = CONVERT(int, @value)
  7180.  
  7181.             /* 
  7182.             ** Check for invalid values 
  7183.             */
  7184.             IF @new_security_mode < 0 OR @new_security_mode > 1
  7185.                 BEGIN
  7186.                     RAISERROR(14109, 16, -1)
  7187.                     RETURN (1)
  7188.                 END
  7189.  
  7190.             IF (UPPER(@publisher) = UPPER(@@SERVERNAME) and ( @platform_nt != platform() & @platform_nt ) and @new_security_mode = 1)
  7191.             BEGIN
  7192.                 RAISERROR(21038, 16, -1)
  7193.                 RETURN (1)
  7194.             END
  7195.  
  7196.             UPDATE msdb..MSdistpublishers SET security_mode = @new_security_mode
  7197.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7198.             IF @@error <> 0 
  7199.                 BEGIN
  7200.                     RETURN (1)
  7201.                 END
  7202.         END
  7203.  
  7204.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'login'
  7205.         BEGIN
  7206.             IF @value IS NULL
  7207.                 BEGIN
  7208.                     RAISERROR (14043, 16, -1, '@value') 
  7209.                     RETURN (1)
  7210.                 END
  7211.  
  7212.             /*
  7213.             ** Set the Login registry key value
  7214.             */
  7215.             SELECT @new_login = CONVERT(sysname, @value)
  7216.  
  7217.             UPDATE msdb..MSdistpublishers SET login = @new_login
  7218.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7219.             IF @@error <> 0 
  7220.                 BEGIN
  7221.                     RETURN (1)
  7222.                 END
  7223.         END
  7224.  
  7225.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'password'
  7226.         BEGIN
  7227.             /*
  7228.             ** Set the Password registry key value
  7229.             */
  7230.             SELECT @new_password = CONVERT(nvarchar(524), @value)
  7231.  
  7232.             -- Encrypt the password
  7233.             EXEC @retcode = master.dbo.xp_repl_encrypt @new_password OUTPUT
  7234.             IF @@error <> 0 OR @retcode <> 0
  7235.                 RETURN (1)
  7236.  
  7237.             UPDATE msdb..MSdistpublishers SET password = @new_password
  7238.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7239.             IF @@error <> 0 
  7240.                 BEGIN
  7241.                     RETURN (1)
  7242.                 END
  7243.          END
  7244.  
  7245.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'active'
  7246.         BEGIN
  7247.             /*
  7248.             ** Check for a valid  value.
  7249.             */
  7250.  
  7251.             IF LOWER(@value collate SQL_Latin1_General_CP1_CS_AS) NOT IN ('true', 'false')
  7252.             BEGIN
  7253.                 RAISERROR (14137, 16, -1)
  7254.                 RETURN (1)
  7255.             END
  7256.  
  7257.             IF LOWER(@value collate SQL_Latin1_General_CP1_CS_AS) = 'true'
  7258.             begin
  7259.                 -- Clean up the database in case of the remote publisher is reinstalling publishing.
  7260.                 SELECT @command = @distribdb + '.dbo.sp_MSdistpublisher_cleanup'
  7261.                 exec @retcode = @command @publisher
  7262.                 if @retcode <> 0 or @@error <> 0
  7263.                     return 1
  7264.                 SELECT @new_active = 1
  7265.             end
  7266.             ELSE
  7267.             BEGIN
  7268.                 SELECT @new_active = 0
  7269.             END
  7270.  
  7271.             /*
  7272.             ** Set the Active registry key value
  7273.             */
  7274.             UPDATE msdb..MSdistpublishers SET active = @new_active
  7275.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7276.             IF @@error <> 0
  7277.                 BEGIN
  7278.                     RETURN (1)
  7279.                 END
  7280.  
  7281.         END
  7282.  
  7283.     IF LOWER(@property collate SQL_Latin1_General_CP1_CS_AS) = 'trusted'
  7284.         BEGIN
  7285.             /*
  7286.             ** Check for a valid  value.
  7287.             */
  7288.  
  7289.             IF LOWER(@value collate SQL_Latin1_General_CP1_CS_AS) NOT IN ('true', 'false')
  7290.             BEGIN
  7291.                 RAISERROR (14137, 16, -1)
  7292.                 RETURN (1)
  7293.             END
  7294.  
  7295.             declare @fExists int
  7296.  
  7297.             IF LOWER(@value collate SQL_Latin1_General_CP1_CS_AS) = 'true'
  7298.             begin
  7299.                 SELECT @new_trusted = 1
  7300.                 exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 'sa'
  7301.                 if( @fExists = 1 )
  7302.                 BEGIN
  7303.                     EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 'sa', trusted, true
  7304.                     IF @@error <> 0 OR @retcode <> 0
  7305.                     BEGIN
  7306.                         RAISERROR (14075, 16, -1)
  7307.                         RETURN (1)
  7308.                     END
  7309.                 END
  7310.  
  7311.  
  7312.                 EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 
  7313.                     'distributor_admin', trusted, true
  7314.                 IF @@error <> 0 OR @retcode <> 0
  7315.                 BEGIN
  7316.                     RAISERROR (14075, 16, -1)
  7317.                     RETURN (1)
  7318.                 END
  7319.             end
  7320.             ELSE
  7321.             BEGIN
  7322.                 SELECT @new_trusted = 0
  7323.      
  7324.                 exec @fExists = dbo.sp_MSIfExistsRemoteLogin @publisher, 'distributor_admin', 'sa'
  7325.                 if( @fExists = 1 )
  7326.                 BEGIN
  7327.                     EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin', 'sa', trusted, false
  7328.                     IF @@error <> 0 OR @retcode <> 0
  7329.                     BEGIN
  7330.                         RAISERROR (14075, 16, -1)
  7331.                         RETURN (1)
  7332.                     END
  7333.                 END
  7334.  
  7335.  
  7336.                 EXECUTE @retcode = dbo.sp_remoteoption @publisher, 'distributor_admin',
  7337.                     'distributor_admin', trusted, 'false'
  7338.                 IF @@error <> 0 OR @retcode <> 0
  7339.                 BEGIN
  7340.                     RAISERROR (14075, 16, -1)
  7341.                     RETURN (1)
  7342.                 END
  7343.             END
  7344.  
  7345.             /*
  7346.             ** Set the trusted property
  7347.             */
  7348.             UPDATE msdb..MSdistpublishers SET trusted = @new_trusted
  7349.                 WHERE UPPER(name) = UPPER(@publisher) collate database_default
  7350.             IF @@error <> 0
  7351.                 BEGIN
  7352.                     RETURN (1)
  7353.                 END
  7354.         END
  7355.  
  7356.   
  7357.     /*
  7358.     ** Return succeed.
  7359.     */
  7360.     RAISERROR (21035, 10, -1, @property)
  7361.  
  7362. DONE:    
  7363.     RETURN (0)
  7364. go
  7365.  
  7366. raiserror('Creating procedure sp_helpdistpublisher', 0,1)
  7367. go
  7368.  
  7369. CREATE PROCEDURE sp_helpdistpublisher (
  7370.     @publisher sysname = '%'         /* publisher server name */
  7371.     , @check_user bit = 0
  7372. AS
  7373. BEGIN
  7374.     SET NOCOUNT ON
  7375.     declare @is_sysadmin int
  7376.  
  7377.     select @is_sysadmin = is_srvrolemember('sysadmin')
  7378.     IF @publisher IS null
  7379.     begin
  7380.         raiserror (14043, 16, -1, '@publisher')
  7381.         return (1)
  7382.     end
  7383.  
  7384.     /*
  7385.     ** Check to make sure this is a distributor
  7386.     */
  7387.     IF NOT EXISTS (select * from master..sysservers
  7388.               where UPPER(datasource) = UPPER(@@SERVERNAME) collate database_default
  7389.                  AND srvstatus & 8 <> 0)
  7390.     begin
  7391.         if @publisher <> '%' 
  7392.         begin
  7393.                 raiserror (14114, 16, -1, @@SERVERNAME)
  7394.                 return(1)
  7395.         end
  7396.         else
  7397.             return(0)
  7398.     end
  7399.  
  7400.     IF @publisher <> '%' AND NOT EXISTS (select * from msdb..MSdistpublishers
  7401.             where UPPER(name) = UPPER(@publisher) collate database_default)
  7402.     begin
  7403.         raiserror (14080, 11, -1)
  7404.         return (1)
  7405.     end
  7406.  
  7407.     -- If sysadmin, no need for filtering
  7408.     if @is_sysadmin = 1
  7409.         select @check_user = 0
  7410.  
  7411.     create table #distdbs (name sysname collate database_default)
  7412.  
  7413.     if @check_user = 1
  7414.     begin
  7415.         declare @distbit int, @db_name sysname, @cmd nvarchar(1000)
  7416.         select @distbit = 16
  7417.         declare hCdatabase CURSOR LOCAL FAST_FORWARD FOR
  7418.             select name from master.dbo.sysdatabases 
  7419.                 where
  7420.                 category & @distbit <> 0 and
  7421.                 has_dbaccess(name) = 1
  7422.         for read only
  7423.  
  7424.         open hCdatabase
  7425.         fetch next from hCdatabase into @db_name
  7426.         while (@@fetch_status <> -1)
  7427.         begin
  7428.             
  7429.             -- Check to see if the user has permision to monitor the distribution 
  7430.             -- database.
  7431.             declare @has_pm bit
  7432.             select @cmd = quotename(@db_name) + '.dbo.sp_executesql'
  7433.             exec @cmd
  7434.                 N'if is_member(N''db_owner'') = 1 or is_member(N''replmonitor'') = 1 set @has_pm = 1', 
  7435.                 N'@has_pm bit output',
  7436.                 @has_pm output
  7437.             if @has_pm = 1
  7438.                 insert #distdbs values (@db_name)
  7439.             fetch next from hCdatabase into @db_name
  7440.         end
  7441.         close hCdatabase
  7442.         deallocate hCdatabase
  7443.     end
  7444.  
  7445.     SELECT p.name, distribution_db, security_mode, 
  7446.         -- Not to return login unless sysadmin is called.
  7447.         'login' = case when (@is_sysadmin = 1) then login else cast(NULL as sysname) end,
  7448.         -- Not to return password unless sysadmin is called.
  7449.         'password' = case  when (@is_sysadmin = 1) then password else cast(NULL as sysname) end,
  7450.         active, working_directory, trusted, thirdparty_flag
  7451.     FROM msdb.dbo.MSdistpublishers p
  7452.     where ((@publisher = N'%') or (UPPER(p.name) collate database_default = UPPER(@publisher) collate database_default)) and
  7453.             (@check_user = 0 or exists 
  7454.                 (select * from #distdbs d where p.distribution_db = d.name collate database_default))
  7455.     IF @@ERROR <> 0
  7456.     begin
  7457.         return(1)
  7458.     end
  7459. END
  7460. GO
  7461.  
  7462. dump tran master with no_log
  7463. go
  7464.  
  7465. raiserror('Creating procedure sp_add_agent_profile', 0,1)
  7466. go
  7467.  
  7468. /*
  7469. ** The system profile of the same type of agent will be used as a template for 
  7470. ** the parameters in this new user profile.
  7471. */
  7472. CREATE PROCEDURE sp_add_agent_profile (
  7473.     @profile_id             int = NULL OUTPUT,
  7474.     @profile_name           sysname,
  7475.     @agent_type             int,            -- 1-Snapshot, 2-Logreader, 
  7476.                                             -- 3-Distribution, 4-Merge,
  7477.                                             -- 9-Qreader
  7478.     @profile_type           int = 1,        -- 0-System, 1-Custom 
  7479.     @description            nvarchar(3000) = NULL,
  7480.     @default                bit = 0         -- 0-Not Default, 1-Default
  7481. )
  7482. AS
  7483.     SET NOCOUNT ON
  7484.  
  7485.     declare     @sys_profile    int
  7486.     declare     @default_id     int
  7487.     declare     @sysdefault_id  int
  7488.     declare     @retcode        int
  7489.  
  7490.     SELECT @profile_name = RTRIM(@profile_name)
  7491.  
  7492.     IF @profile_name IS NULL
  7493.     BEGIN
  7494.         RAISERROR (14043, 16, -1, '@profile_name')
  7495.         RETURN (1)
  7496.     END
  7497.  
  7498.     exec @retcode = dbo.sp_MSreplcheck_name @profile_name
  7499.     if @@ERROR <> 0 or @retcode <> 0
  7500.         return(1)
  7501.  
  7502.     /* The profile name is unique across a particular agent type */
  7503.     IF EXISTS ( SELECT * FROM msdb..MSagent_profiles 
  7504.             WHERE profile_name = @profile_name collate database_default
  7505.             AND agent_type = @agent_type )
  7506.     BEGIN
  7507.         RAISERROR(20057, 16, -1, @profile_name)
  7508.         RETURN (1)
  7509.     END
  7510.  
  7511.     IF @agent_type NOT IN (1, 2, 3, 4, 9)
  7512.     BEGIN
  7513.         RAISERROR(20058, 16, -1)
  7514.         return (1)
  7515.     END
  7516.  
  7517.     IF @profile_type NOT IN (0, 1)
  7518.     BEGIN
  7519.         RAISERROR(20059, 16, -1)
  7520.         return (1)
  7521.     END
  7522.  
  7523.     BEGIN TRAN
  7524.  
  7525.     /*
  7526.     ** First find out the default profile of the same agent type
  7527.     */
  7528.     select @default_id = profile_id from msdb..MSagent_profiles WHERE agent_type = @agent_type AND def_profile = 1
  7529.     
  7530.     INSERT INTO msdb..MSagent_profiles
  7531.             VALUES (@profile_name, @agent_type, @profile_type, @description, 0)
  7532.     IF @@ERROR <> 0
  7533.         GOTO UNDO
  7534.  
  7535.     SELECT @profile_id = profile_id 
  7536.         FROM msdb..MSagent_profiles
  7537.         WHERE profile_name = @profile_name collate database_default AND agent_type = @agent_type
  7538.  
  7539.     /*
  7540.     ** If there is system profiles of the same agent type, use the primary system one as template of parameter set
  7541.     */
  7542.     IF EXISTS (select * from msdb..MSagent_profiles where agent_type = @agent_type AND type = 0)
  7543.         select @sysdefault_id = min(profile_id) from msdb..MSagent_profiles where agent_type = @agent_type AND type = 0
  7544.  
  7545.     if @sysdefault_id IS NULL
  7546.         select @sysdefault_id = @default_id
  7547.  
  7548.     if @profile_type=1
  7549.     BEGIN
  7550.             INSERT INTO msdb..MSagent_parameters 
  7551.             select @profile_id, parameter_name, value from msdb..MSagent_parameters 
  7552.                 where profile_id = @sysdefault_id      
  7553.         if @@ERROR<>0 
  7554.             GOTO UNDO
  7555.     END
  7556.  
  7557.     /* Only one profile for an agent type must be default */
  7558.     IF @default = 1
  7559.         BEGIN
  7560.             UPDATE msdb..MSagent_profiles
  7561.                 SET def_profile = 0 WHERE profile_id = @default_id
  7562.             IF @@ERROR <> 0
  7563.                 GOTO UNDO
  7564.             UPDATE msdb..MSagent_profiles
  7565.                 SET def_profile = 1 WHERE profile_id = @profile_id
  7566.             IF @@ERROR <> 0
  7567.                 GOTO UNDO
  7568.         END
  7569.  
  7570.     COMMIT TRAN
  7571.     RETURN 0 
  7572.  
  7573. UNDO:
  7574.  
  7575.     IF @@TRANCOUNT = 1
  7576.         ROLLBACK TRAN
  7577.     ELSE
  7578.         COMMIT TRAN
  7579.  
  7580.     RETURN 1 
  7581. GO
  7582.  
  7583. raiserror('Creating procedure sp_drop_agent_parameter', 0,1)
  7584. go
  7585.  
  7586. -- Drop a/all parameter(s) of a/all profile(s) from the MSagent_parameters table
  7587. create procedure sp_drop_agent_parameter (
  7588.     @profile_id int,
  7589.     @parameter_name     sysname = '%'
  7590. )
  7591. as
  7592.     set nocount on
  7593.  
  7594.     declare @default            bit
  7595.     declare @agent_type         int
  7596.     declare @retstatus          int
  7597.     select @retstatus = 0
  7598.  
  7599.     if @parameter_name is null
  7600.     BEGIN
  7601.         RAISERROR (14043, 16, -1, '@parameter_name')
  7602.         RETURN (1)
  7603.     END
  7604.  
  7605.     select @agent_type = agent_type, @default=def_profile
  7606.     from msdb..MSagent_profiles
  7607.     where profile_id = @profile_id
  7608.  
  7609.     if @default is null
  7610.     BEGIN
  7611.         RAISERROR (20066, 16, -1) -- profile not defined
  7612.         RETURN (1)
  7613.     END
  7614.     
  7615.     select @parameter_name = lower(@parameter_name collate SQL_Latin1_General_CP1_CS_AS)
  7616.  
  7617.     if left(@parameter_name,1) in (N'-', N'/')
  7618.         select @parameter_name = right(@parameter_name,len(@parameter_name)-1)
  7619.  
  7620.     /* Delete the parameters of the profile */
  7621.     delete  msdb..MSagent_parameters 
  7622.     where   right(lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS),len(parameter_name)-1) like 
  7623.             @parameter_name collate SQL_Latin1_General_CP1_CS_AS
  7624.     and     profile_id = @profile_id
  7625.  
  7626.     if @@error <> 0
  7627.     begin
  7628.         select @retstatus = 1
  7629.         goto UNDO
  7630.     end
  7631.  
  7632. UNDO:
  7633.     
  7634.     return @retstatus
  7635. GO
  7636.  
  7637. raiserror('Creating procedure sp_drop_agent_profile', 0,1)
  7638. go
  7639. -- Drop a profile from the MSagent_profiles table, as well as the corresponding
  7640. -- parameters from the MSagent_parameters table
  7641.  
  7642. CREATE PROCEDURE sp_drop_agent_profile (
  7643.     @profile_id int
  7644. )
  7645. AS
  7646.     SET NOCOUNT ON
  7647.  
  7648.     declare @snapshot_type      int
  7649.     declare @logreader_type     int
  7650.     declare @distribution_type  int
  7651.     declare @merge_type         int
  7652.     declare @qreader_type        int
  7653.     
  7654.     declare @tablename          nvarchar(255)
  7655.     declare @proc               nvarchar(255)
  7656.     declare @distribution_db    sysname
  7657.     declare @profile_type       int
  7658.  
  7659.     declare @default            bit
  7660.     declare @usage_count        int
  7661.     declare @agent_type         int
  7662.     declare @retstatus          int
  7663.     DECLARE @retcode            int
  7664.  
  7665.     declare @default_sys_id     int
  7666.  
  7667.     SELECT @agent_type = agent_type, @profile_type = type, @default=def_profile
  7668.     FROM msdb..MSagent_profiles
  7669.     WHERE profile_id = @profile_id
  7670.  
  7671.     IF @default IS NULL
  7672.     BEGIN
  7673.         RAISERROR (20066, 16, -1) -- Profile not defined
  7674.         RETURN (1)
  7675.     END
  7676.  
  7677.     /*
  7678.     ** Before dropping a default profile, one system profile of the same agent type
  7679.     ** has to become the new default profile.
  7680.     */
  7681.     IF @default = 1
  7682.     BEGIN
  7683.         select @default_sys_id=min(profile_id) from msdb..MSagent_profiles
  7684.             where agent_type = @agent_type AND type = 0
  7685.             
  7686.         UPDATE msdb..MSagent_profiles SET def_profile = 1 WHERE profile_id = @default_sys_id
  7687.     END
  7688.  
  7689.     select @snapshot_type = 1
  7690.     select @logreader_type = 2
  7691.     select @distribution_type = 3
  7692.     select @merge_type = 4
  7693.     select @qreader_type = 9
  7694.  
  7695.         /* By default, assume that this profile is not being used */
  7696.     select @usage_count = -1
  7697.  
  7698.     select @tablename = 
  7699.         case @agent_type
  7700.             when @snapshot_type then 'MSsnapshot_agents'
  7701.             when @logreader_type then 'MSlogreader_agents'
  7702.             when @distribution_type then 'MSdistribution_agents'
  7703.             when @merge_type then 'MSmerge_agents'
  7704.             when @qreader_type then 'MSqreader_agents'
  7705.         end
  7706.  
  7707.     declare hCdistdbs CURSOR LOCAL FAST_FORWARD FOR
  7708.         select distinct distribution_db 
  7709.         from msdb..MSdistpublishers
  7710.         for read only
  7711.  
  7712.     open hCdistdbs
  7713.     fetch hCdistdbs into @distribution_db
  7714.  
  7715.     while @@fetch_status <> -1 and @usage_count = -1
  7716.     begin
  7717.         select @proc = @distribution_db + '.dbo.sp_MSprofile_in_use'
  7718.         execute @usage_count = @proc @tablename = @tablename, @profile_id = @profile_id
  7719.     
  7720.         if @@error <> 0
  7721.         begin
  7722.             select @retstatus = 1
  7723.             goto UNDO
  7724.         end
  7725.  
  7726.         fetch hCdistdbs into @distribution_db
  7727.     end
  7728.  
  7729.     /* A profile in use cannot be dropped */
  7730.     if @usage_count = 0 
  7731.     begin
  7732.         RAISERROR(20065, 16, -1) -- Cannot drop profile, because it is in use.
  7733.         select @retstatus = 1
  7734.         goto UNDO 
  7735.     end
  7736.  
  7737.     BEGIN TRAN
  7738.  
  7739.     /*****
  7740.      * NOTE : If sp_drop_agent_parameter fails, the profile must not be 
  7741.      * deleted either 
  7742.      */
  7743.     EXECUTE @retcode = dbo.sp_drop_agent_parameter @profile_id = @profile_id,
  7744.                             @parameter_name = '%'
  7745.  
  7746.     IF @@ERROR <> 0 OR @retcode <> 0
  7747.         GOTO UNDO
  7748.             
  7749.     /* Delete all the entries in one go */
  7750.     DELETE msdb..MSagent_profiles 
  7751.     WHERE profile_id = @profile_id
  7752.  
  7753.     IF @@ERROR <> 0
  7754.         GOTO UNDO
  7755.  
  7756.     COMMIT TRAN
  7757.  
  7758.     close hCdistdbs
  7759.     deallocate hCdistdbs
  7760.  
  7761.     RETURN 0
  7762.  
  7763. UNDO:
  7764.     IF @@TRANCOUNT = 1
  7765.         ROLLBACK TRAN
  7766.     ELSE IF @@TRANCOUNT > 1 -- Sometimes we can get here when @@trancount = 0, so need to check explicitly.
  7767.         COMMIT TRAN
  7768.  
  7769.     close hCdistdbs
  7770.     deallocate hCdistdbs
  7771.  
  7772.  
  7773.     RETURN (1)
  7774. GO
  7775.  
  7776. raiserror('Creating procedure sp_help_agent_profile', 0,1)
  7777. go
  7778.  
  7779.  
  7780. -- View the row(s) of the MSagent_profiles table
  7781.  
  7782. create procedure sp_help_agent_profile(
  7783.     @agent_type     int = 0,
  7784.     @profile_id int = -1
  7785. )
  7786. as
  7787.     set nocount on
  7788.  
  7789.     declare @proc nvarchar(255)
  7790.  
  7791.     declare @snapshot_type      int
  7792.     declare @logreader_type     int
  7793.     declare @distribution_type  int
  7794.     declare @merge_type     int
  7795.     declare @qreader_type        int
  7796.  
  7797.     select @snapshot_type = 1
  7798.     select @logreader_type = 2
  7799.     select @distribution_type = 3
  7800.     select @merge_type = 4
  7801.     select @qreader_type = 9
  7802.  
  7803.     if @agent_type is null 
  7804.     BEGIN
  7805.         RAISERROR (14043, 16, -1, '@agent_type')
  7806.         RETURN (1)
  7807.     END
  7808.  
  7809.     if @profile_id is null
  7810.     BEGIN
  7811.         RAISERROR (14043, 16, -1, '@profile_id')
  7812.         RETURN (1)
  7813.     END
  7814.  
  7815.     if @agent_type not in (0, @snapshot_type, @logreader_type, @distribution_type, @merge_type, @qreader_type)
  7816.     BEGIN
  7817.         RAISERROR(20058, 16, -1)
  7818.         return (1)
  7819.     END
  7820.  
  7821.     select profile_id, profile_name, agent_type, type, description, def_profile
  7822.         from msdb..MSagent_profiles where
  7823.              (profile_id = @profile_id or @profile_id = -1) and
  7824.              (agent_type = @agent_type or @agent_type = 0)
  7825.         order by profile_id, profile_name
  7826.  
  7827. GO
  7828.  
  7829. raiserror('Creating procedure sp_help_agent_default', 0,1)
  7830. go
  7831.  
  7832. create procedure sp_help_agent_default (
  7833.     @profile_id     int OUTPUT, 
  7834.     @agent_type         int
  7835. )
  7836. as
  7837.     set nocount on
  7838.  
  7839.     if @agent_type not in (1, 2, 3, 4)
  7840.     BEGIN
  7841.         RAISERROR(20058, 16, -1)
  7842.         return (1)
  7843.     END
  7844.  
  7845.  
  7846.     select @profile_id = profile_id 
  7847.     from msdb..MSagent_profiles 
  7848.     where agent_type = @agent_type
  7849.     and def_profile = 1 
  7850. go
  7851.  
  7852. raiserror('Creating procedure sp_MSupdate_agenttype_default', 0,1)
  7853. go
  7854.  
  7855. -- Set a profile as a default for an agent_type
  7856. create procedure sp_MSupdate_agenttype_default(
  7857.     @profile_id int
  7858. )
  7859. as
  7860.     set nocount on
  7861.  
  7862.     declare @agent_type int
  7863.  
  7864.     /* This profile must have been defined for this agent type */
  7865.     if not exists ( select * from msdb..MSagent_profiles
  7866.             where profile_id = @profile_id )
  7867.     BEGIN
  7868.         RAISERROR (20066, 16, -1)   -- profile not defined
  7869.         RETURN (1)
  7870.     END
  7871.  
  7872.  
  7873.     select @agent_type = agent_type
  7874.     from msdb..MSagent_profiles 
  7875.     where profile_id = @profile_id
  7876.  
  7877.     BEGIN TRAN
  7878.  
  7879.     update msdb..MSagent_profiles
  7880.     set def_profile = 0
  7881.     where agent_type = @agent_type
  7882.     and def_profile = 1
  7883.  
  7884.     if @@error <> 0
  7885.         goto UNDO
  7886.  
  7887.     update msdb..MSagent_profiles
  7888.     set def_profile = 1
  7889.     where profile_id = @profile_id
  7890.  
  7891.     if @@error <> 0
  7892.         goto UNDO 
  7893.  
  7894.     COMMIT TRAN
  7895.  
  7896.     return 0
  7897.  
  7898. UNDO:
  7899.     if @@trancount = 1
  7900.         rollback tran
  7901.     else
  7902.         commit tran
  7903.  
  7904.     return 1 
  7905. GO
  7906.  
  7907.  
  7908. /*
  7909. ** Create stored procedures to generate default profile 
  7910. ** parameters for replication agents.
  7911. */
  7912.  
  7913. raiserror('Creating procedure sp_generate_agent_parameter', 0,1)
  7914. GO
  7915.  
  7916. create procedure sp_generate_agent_parameter(
  7917.     @profile_id     int,        -- Fixed profile id used
  7918.     @real_profile_id int = NULL -- Used by version upgrade 
  7919. )
  7920. as
  7921.     declare @retcode    int
  7922.  
  7923.     -- If real profile id is not passed in. Use the fix profile id.
  7924.     if @real_profile_id is NULL
  7925.         select @real_profile_id = @profile_id
  7926.  
  7927.         /* 
  7928.     ** Snapshot agent 
  7929.     */
  7930.     if (@profile_id = 1)
  7931.         begin
  7932.         exec @retcode = dbo.sp_add_agent_parameter 
  7933.                 @profile_id = @real_profile_id,
  7934.                 @parameter_name = '-BcpBatchSize',
  7935.                 @parameter_value = '100000'
  7936.         if (@retcode = 1 or @@ERROR <> 0)
  7937.             goto FAILURE
  7938.  
  7939.         exec @retcode = dbo.sp_add_agent_parameter 
  7940.                 @profile_id = @real_profile_id,
  7941.                 @parameter_name = '-HistoryVerboseLevel',
  7942.                 @parameter_value = '2'
  7943.         if (@retcode = 1 or @@ERROR <> 0)
  7944.             goto FAILURE
  7945.  
  7946.         exec @retcode = dbo.sp_add_agent_parameter 
  7947.                 @profile_id = @real_profile_id,
  7948.                 @parameter_name = '-LoginTimeout',
  7949.                 @parameter_value = '15'
  7950.         if (@retcode = 1 or @@ERROR <> 0)
  7951.             goto FAILURE
  7952.  
  7953.         exec @retcode = dbo.sp_add_agent_parameter 
  7954.                 @profile_id = @real_profile_id,
  7955.                 @parameter_name = '-MaxBcpThreads',
  7956.                 @parameter_value = '1'
  7957.         if (@retcode = 1 or @@ERROR <> 0)
  7958.             goto FAILURE
  7959.  
  7960.         exec @retcode = dbo.sp_add_agent_parameter 
  7961.                 @profile_id = @real_profile_id,
  7962.                 @parameter_name = '-QueryTimeout',
  7963.                 @parameter_value = '300'        -- 5 minutes 
  7964.         if (@retcode = 1 or @@ERROR <> 0)
  7965.             goto FAILURE
  7966.         end
  7967.  
  7968.         /* 
  7969.     ** Logreader agent 
  7970.     */
  7971.     else if (@profile_id = 2)
  7972.         begin
  7973.   
  7974.         exec @retcode = dbo.sp_add_agent_parameter 
  7975.                 @profile_id = @real_profile_id,
  7976.                 @parameter_name = '-HistoryVerboseLevel',
  7977.                 @parameter_value = '1'
  7978.         if (@retcode = 1 or @@ERROR <> 0)
  7979.             goto FAILURE
  7980.  
  7981.         exec @retcode = dbo.sp_add_agent_parameter 
  7982.                 @profile_id = @real_profile_id,
  7983.                 @parameter_name = '-LoginTimeout',
  7984.                 @parameter_value = '15'
  7985.         if (@retcode = 1 or @@ERROR <> 0)
  7986.             goto FAILURE
  7987.  
  7988.         exec @retcode = dbo.sp_add_agent_parameter 
  7989.                 @profile_id = @real_profile_id,
  7990.                 @parameter_name = '-PollingInterval',
  7991.                 @parameter_value = '10'
  7992.         if (@retcode = 1 or @@ERROR <> 0)
  7993.             goto FAILURE
  7994.  
  7995.         exec @retcode = dbo.sp_add_agent_parameter 
  7996.                 @profile_id = @real_profile_id,
  7997.                 @parameter_name = '-QueryTimeout',
  7998.                 @parameter_value = '300'        -- 5 minutes
  7999.         if (@retcode = 1 or @@ERROR <> 0)
  8000.             goto FAILURE
  8001.  
  8002.         exec @retcode = dbo.sp_add_agent_parameter 
  8003.                 @profile_id = @real_profile_id,
  8004.                 @parameter_name = '-ReadBatchSize',
  8005.                 @parameter_value = '500'
  8006.         if (@retcode = 1 or @@ERROR <> 0)
  8007.             goto FAILURE
  8008.  
  8009.         end
  8010.  
  8011.         /* 
  8012.     ** Logreader agent - Verbose History Profile
  8013.     */
  8014.     else if (@profile_id = 3)
  8015.         begin
  8016.  
  8017.         exec @retcode = dbo.sp_add_agent_parameter 
  8018.                 @profile_id = @real_profile_id,
  8019.                 @parameter_name = '-HistoryVerboseLevel',
  8020.                 @parameter_value = '2'
  8021.         if (@retcode = 1 or @@ERROR <> 0)
  8022.             goto FAILURE
  8023.  
  8024.         exec @retcode = dbo.sp_add_agent_parameter 
  8025.                 @profile_id = @real_profile_id,
  8026.                 @parameter_name = '-LoginTimeout',
  8027.                 @parameter_value = '15'
  8028.         if (@retcode = 1 or @@ERROR <> 0)
  8029.             goto FAILURE
  8030.  
  8031.         exec @retcode = dbo.sp_add_agent_parameter 
  8032.                 @profile_id = @real_profile_id,
  8033.                 @parameter_name = '-PollingInterval',
  8034.                 @parameter_value = '10'
  8035.         if (@retcode = 1 or @@ERROR <> 0)
  8036.             goto FAILURE
  8037.  
  8038.         exec @retcode = dbo.sp_add_agent_parameter 
  8039.                 @profile_id = @real_profile_id,
  8040.                 @parameter_name = '-QueryTimeout',
  8041.                 @parameter_value = '300'        -- 5 minutes
  8042.         if (@retcode = 1 or @@ERROR <> 0)
  8043.             goto FAILURE
  8044.  
  8045.         exec @retcode = dbo.sp_add_agent_parameter 
  8046.                 @profile_id = @real_profile_id,
  8047.                 @parameter_name = '-ReadBatchSize',
  8048.                 @parameter_value = '500'
  8049.         if (@retcode = 1 or @@ERROR <> 0)
  8050.             goto FAILURE
  8051.  
  8052.         end
  8053.  
  8054.         /* 
  8055.     ** Distribution agent 
  8056.     */
  8057.     else if (@profile_id = 4)
  8058.       begin
  8059.  
  8060.         exec @retcode = dbo.sp_add_agent_parameter 
  8061.                 @profile_id = @real_profile_id,
  8062.                 @parameter_name = '-BcpBatchSize',
  8063.                 @parameter_value = '100000'
  8064.         if (@retcode = 1 or @@ERROR <> 0)
  8065.             goto FAILURE
  8066.  
  8067.         exec @retcode = dbo.sp_add_agent_parameter 
  8068.                 @profile_id = @real_profile_id,
  8069.                 @parameter_name = '-CommitBatchSize',
  8070.                 @parameter_value = '100'
  8071.         if (@retcode = 1 or @@ERROR <> 0)
  8072.             goto FAILURE
  8073.  
  8074.         exec @retcode = dbo.sp_add_agent_parameter 
  8075.                 @profile_id = @real_profile_id,
  8076.                 @parameter_name = '-CommitBatchThreshold',
  8077.                 @parameter_value = '1000'
  8078.         if (@retcode = 1 or @@ERROR <> 0)
  8079.             goto FAILURE
  8080.  
  8081.         exec @retcode = dbo.sp_add_agent_parameter 
  8082.                 @profile_id = @real_profile_id,
  8083.                 @parameter_name = '-HistoryVerboseLevel',
  8084.                 @parameter_value = '1'
  8085.         if (@retcode = 1 or @@ERROR <> 0)
  8086.             goto FAILURE
  8087.  
  8088.         exec @retcode = dbo.sp_add_agent_parameter 
  8089.                 @profile_id = @real_profile_id,
  8090.                 @parameter_name = '-LoginTimeout',
  8091.                 @parameter_value = '15'
  8092.         if (@retcode = 1 or @@ERROR <> 0)
  8093.             goto FAILURE
  8094.  
  8095.         exec @retcode = dbo.sp_add_agent_parameter 
  8096.                 @profile_id = @real_profile_id,
  8097.                 @parameter_name = '-MaxBcpThreads',
  8098.                 @parameter_value = '1'
  8099.         if (@retcode = 1 or @@ERROR <> 0)
  8100.             goto FAILURE
  8101.  
  8102.         exec @retcode = dbo.sp_add_agent_parameter 
  8103.                 @profile_id = @real_profile_id,
  8104.                 @parameter_name = '-MaxDeliveredTransactions',
  8105.                 @parameter_value = '0'
  8106.         if (@retcode = 1 or @@ERROR <> 0)
  8107.             goto FAILURE
  8108.  
  8109.         exec @retcode = dbo.sp_add_agent_parameter 
  8110.                 @profile_id = @real_profile_id,
  8111.                 @parameter_name = '-PollingInterval',
  8112.                 @parameter_value = '10'
  8113.         if (@retcode = 1 or @@ERROR <> 0)
  8114.             goto FAILURE
  8115.  
  8116.         exec @retcode = dbo.sp_add_agent_parameter 
  8117.                 @profile_id = @real_profile_id,
  8118.                 @parameter_name = '-QueryTimeout',
  8119.                 @parameter_value = '300'        -- 5 minutes 
  8120.         if (@retcode = 1 or @@ERROR <> 0)
  8121.             goto FAILURE
  8122.  
  8123.         exec @retcode = dbo.sp_add_agent_parameter 
  8124.                 @profile_id = @real_profile_id,
  8125.                 @parameter_name = '-TransactionsPerHistory',
  8126.                 @parameter_value = '100'
  8127.         if (@retcode = 1 or @@ERROR <> 0)
  8128.             goto FAILURE
  8129.  
  8130.         exec @retcode = dbo.sp_add_agent_parameter 
  8131.                 @profile_id = @real_profile_id,
  8132.                 @parameter_name = '-SkipErrors',
  8133.                 @parameter_value = ''
  8134.         if (@retcode = 1 or @@ERROR <> 0)
  8135.             goto FAILURE
  8136.  
  8137.         exec @retcode = dbo.sp_add_agent_parameter
  8138.                 @profile_id = @real_profile_id,
  8139.                 @parameter_name = '-KeepAliveMessageInterval',
  8140.                 @parameter_value = '300'
  8141.         if (@retcode = 1 or @@ERROR <> 0)
  8142.             goto FAILURE
  8143.  
  8144.         end
  8145.  
  8146.     /*
  8147.     ** Distribution Agent Verbose History Profile
  8148.     */
  8149.     else if (@profile_id = 5)
  8150.       begin
  8151.  
  8152.         exec @retcode = dbo.sp_add_agent_parameter 
  8153.                 @profile_id = @real_profile_id,
  8154.                 @parameter_name = '-BcpBatchSize',
  8155.                 @parameter_value = '100000'
  8156.         if (@retcode = 1 or @@ERROR <> 0)
  8157.             goto FAILURE
  8158.  
  8159.         exec @retcode = dbo.sp_add_agent_parameter 
  8160.                 @profile_id = @real_profile_id,
  8161.                 @parameter_name = '-CommitBatchSize',
  8162.                 @parameter_value = '100'
  8163.         if (@retcode = 1 or @@ERROR <> 0)
  8164.             goto FAILURE
  8165.  
  8166.         exec @retcode = dbo.sp_add_agent_parameter 
  8167.                 @profile_id = @real_profile_id,
  8168.                 @parameter_name = '-CommitBatchThreshold',
  8169.                 @parameter_value = '1000'
  8170.         if (@retcode = 1 or @@ERROR <> 0)
  8171.             goto FAILURE
  8172.  
  8173.         exec @retcode = dbo.sp_add_agent_parameter 
  8174.                 @profile_id = @real_profile_id,
  8175.                 @parameter_name = '-HistoryVerboseLevel',
  8176.                 @parameter_value = '2'
  8177.         if (@retcode = 1 or @@ERROR <> 0)
  8178.             goto FAILURE
  8179.  
  8180.         exec @retcode = dbo.sp_add_agent_parameter 
  8181.                 @profile_id = @real_profile_id,
  8182.                 @parameter_name = '-LoginTimeout',
  8183.                 @parameter_value = '15'
  8184.         if (@retcode = 1 or @@ERROR <> 0)
  8185.             goto FAILURE
  8186.  
  8187.         exec @retcode = dbo.sp_add_agent_parameter 
  8188.                 @profile_id = @real_profile_id,
  8189.                 @parameter_name = '-MaxBcpThreads',
  8190.                 @parameter_value = '1'
  8191.         if (@retcode = 1 or @@ERROR <> 0)
  8192.             goto FAILURE
  8193.  
  8194.         exec @retcode = dbo.sp_add_agent_parameter 
  8195.                 @profile_id = @real_profile_id,
  8196.                 @parameter_name = '-MaxDeliveredTransactions',
  8197.                 @parameter_value = '0'
  8198.         if (@retcode = 1 or @@ERROR <> 0)
  8199.             goto FAILURE
  8200.  
  8201.         exec @retcode = dbo.sp_add_agent_parameter 
  8202.                 @profile_id = @real_profile_id,
  8203.                 @parameter_name = '-PollingInterval',
  8204.                 @parameter_value = '10'
  8205.         if (@retcode = 1 or @@ERROR <> 0)
  8206.             goto FAILURE
  8207.  
  8208.         exec @retcode = dbo.sp_add_agent_parameter 
  8209.                 @profile_id = @real_profile_id,
  8210.                 @parameter_name = '-QueryTimeout',
  8211.                 @parameter_value = '300'        -- 5 minutes 
  8212.         if (@retcode = 1 or @@ERROR <> 0)
  8213.             goto FAILURE
  8214.  
  8215.         exec @retcode = dbo.sp_add_agent_parameter 
  8216.                 @profile_id = @real_profile_id,
  8217.                 @parameter_name = '-TransactionsPerHistory',
  8218.                 @parameter_value = '100'
  8219.         if (@retcode = 1 or @@ERROR <> 0)
  8220.             goto FAILURE
  8221.  
  8222.         exec @retcode = dbo.sp_add_agent_parameter 
  8223.                 @profile_id = @real_profile_id,
  8224.                 @parameter_name = '-SkipErrors',
  8225.                 @parameter_value = ''
  8226.         if (@retcode = 1 or @@ERROR <> 0)
  8227.             goto FAILURE
  8228.  
  8229.         exec @retcode = dbo.sp_add_agent_parameter
  8230.                 @profile_id = @real_profile_id,
  8231.                 @parameter_name = '-KeepAliveMessageInterval',
  8232.                 @parameter_value = '300'
  8233.         if (@retcode = 1 or @@ERROR <> 0)
  8234.             goto FAILURE
  8235.  
  8236.         end
  8237.     /* 
  8238.     ** Merge agent : Default profile for well connected scenarios 
  8239.     */
  8240.     else if (@profile_id = 6)
  8241.         begin
  8242.         exec @retcode = dbo.sp_add_agent_parameter 
  8243.                 @profile_id = @real_profile_id,
  8244.                 @parameter_name = '-PollingInterval',
  8245.                 @parameter_value = '60'
  8246.         if (@retcode = 1 or @@ERROR <> 0)
  8247.             goto FAILURE
  8248.  
  8249.         exec @retcode = dbo.sp_add_agent_parameter 
  8250.                 @profile_id = @real_profile_id,
  8251.                 @parameter_name = '-StartQueueTimeout',
  8252.                 @parameter_value = '0'
  8253.         if (@retcode = 1 or @@ERROR <> 0)
  8254.             goto FAILURE
  8255.  
  8256.         exec @retcode = dbo.sp_add_agent_parameter 
  8257.                 @profile_id = @real_profile_id,
  8258.                 @parameter_name = '-MaxDownloadChanges',
  8259.                 @parameter_value = '0'
  8260.         if (@retcode = 1 or @@ERROR <> 0)
  8261.             goto FAILURE
  8262.  
  8263.         exec @retcode = dbo.sp_add_agent_parameter 
  8264.                 @profile_id = @real_profile_id,
  8265.                 @parameter_name = '-MaxUploadChanges',
  8266.                 @parameter_value = '0'
  8267.         if (@retcode = 1 or @@ERROR <> 0)
  8268.             goto FAILURE
  8269.  
  8270.         exec @retcode = dbo.sp_add_agent_parameter 
  8271.                 @profile_id = @real_profile_id,
  8272.                 @parameter_name = '-ValidateInterval',
  8273.                 @parameter_value = '60'
  8274.         if (@retcode = 1 or @@ERROR <> 0)
  8275.             goto FAILURE
  8276.  
  8277.         exec @retcode = dbo.sp_add_agent_parameter 
  8278.                 @profile_id = @real_profile_id,
  8279.                 @parameter_name = '-LoginTimeout',
  8280.                 @parameter_value = '15'
  8281.         if (@retcode = 1 or @@ERROR <> 0)
  8282.             goto FAILURE
  8283.  
  8284.         exec @retcode = dbo.sp_add_agent_parameter 
  8285.                 @profile_id = @real_profile_id,
  8286.                 @parameter_name = '-QueryTimeout',
  8287.                 @parameter_value = '300'        
  8288.         if (@retcode = 1 or @@ERROR <> 0)
  8289.             goto FAILURE
  8290.  
  8291.         exec @retcode = dbo.sp_add_agent_parameter 
  8292.                 @profile_id = @real_profile_id,
  8293.                 @parameter_name = '-UploadGenerationsPerBatch',
  8294.                 @parameter_value = '100'
  8295.         if (@retcode = 1 or @@ERROR <> 0)
  8296.             goto FAILURE
  8297.  
  8298.         exec @retcode = dbo.sp_add_agent_parameter 
  8299.                 @profile_id = @real_profile_id,
  8300.                 @parameter_name = '-DownloadGenerationsPerBatch',
  8301.                 @parameter_value = '100'
  8302.         if (@retcode = 1 or @@ERROR <> 0)
  8303.             goto FAILURE
  8304.  
  8305.         exec @retcode = dbo.sp_add_agent_parameter 
  8306.                 @profile_id = @real_profile_id,
  8307.                 @parameter_name = '-UploadReadChangesPerBatch',
  8308.                 @parameter_value = '100'
  8309.         if (@retcode = 1 or @@ERROR <> 0)
  8310.             goto FAILURE
  8311.  
  8312.         exec @retcode = dbo.sp_add_agent_parameter 
  8313.                 @profile_id = @real_profile_id,
  8314.                 @parameter_name = '-DownloadReadChangesPerBatch',
  8315.                 @parameter_value = '100'
  8316.         if (@retcode = 1 or @@ERROR <> 0)
  8317.             goto FAILURE
  8318.  
  8319.         exec @retcode = dbo.sp_add_agent_parameter 
  8320.                 @profile_id = @real_profile_id,
  8321.                 @parameter_name = '-UploadWriteChangesPerBatch',
  8322.                 @parameter_value = '100'
  8323.         if (@retcode = 1 or @@ERROR <> 0)
  8324.             goto FAILURE
  8325.  
  8326.         exec @retcode = dbo.sp_add_agent_parameter 
  8327.                 @profile_id = @real_profile_id,
  8328.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  8329.                 @parameter_value = '100'
  8330.         if (@retcode = 1 or @@ERROR <> 0)
  8331.             goto FAILURE
  8332.  
  8333.         exec @retcode = dbo.sp_add_agent_parameter 
  8334.                 @profile_id = @real_profile_id,
  8335.                 @parameter_name = '-Validate',
  8336.                 @parameter_value = '0'
  8337.         if (@retcode = 1 or @@ERROR <> 0)
  8338.             goto FAILURE
  8339.  
  8340.         exec @retcode = dbo.sp_add_agent_parameter 
  8341.                 @profile_id = @real_profile_id,
  8342.                 @parameter_name = '-FastRowCount',
  8343.                 @parameter_value = '1'
  8344.         if (@retcode = 1 or @@ERROR <> 0)
  8345.             goto FAILURE
  8346.         
  8347.         exec @retcode = dbo.sp_add_agent_parameter 
  8348.                 @profile_id = @real_profile_id,
  8349.                 @parameter_name = '-HistoryVerboseLevel',
  8350.                 @parameter_value = '1'
  8351.         if (@retcode = 1 or @@ERROR <> 0)
  8352.             goto FAILURE
  8353.  
  8354.         exec @retcode = dbo.sp_add_agent_parameter 
  8355.                 @profile_id = @real_profile_id,
  8356.                 @parameter_name = '-ChangesPerHistory',
  8357.                 @parameter_value = '100'
  8358.         if (@retcode = 1 or @@ERROR <> 0)
  8359.             goto FAILURE
  8360.  
  8361.         exec @retcode = dbo.sp_add_agent_parameter 
  8362.                 @profile_id = @real_profile_id,
  8363.                 @parameter_name = '-BcpBatchSize',
  8364.                 @parameter_value = '100000'
  8365.         if (@retcode = 1 or @@ERROR <> 0)
  8366.             goto FAILURE
  8367.  
  8368.         exec @retcode = dbo.sp_add_agent_parameter 
  8369.                 @profile_id = @real_profile_id,
  8370.                 @parameter_name = '-NumDeadlockRetries',
  8371.                 @parameter_value = '5'
  8372.         if (@retcode = 1 or @@ERROR <> 0)
  8373.             goto FAILURE
  8374.  
  8375.         exec @retcode = dbo.sp_add_agent_parameter 
  8376.                 @profile_id = @real_profile_id,
  8377.                 @parameter_name = '-SrcThreads',
  8378.                 @parameter_value = '3'
  8379.         if (@retcode = 1 or @@ERROR <> 0)
  8380.             goto FAILURE
  8381.  
  8382.         exec @retcode = dbo.sp_add_agent_parameter 
  8383.                 @profile_id = @real_profile_id,
  8384.                 @parameter_name = '-DestThreads',
  8385.                 @parameter_value = '4'
  8386.         if (@retcode = 1 or @@ERROR <> 0)
  8387.             goto FAILURE
  8388.  
  8389.         exec @retcode = dbo.sp_add_agent_parameter
  8390.                 @profile_id = @real_profile_id,
  8391.                 @parameter_name = '-KeepAliveMessageInterval',
  8392.                 @parameter_value = '300'
  8393.         if (@retcode = 1 or @@ERROR <> 0)
  8394.             goto FAILURE
  8395.  
  8396.         exec @retcode = dbo.sp_add_agent_parameter
  8397.                 @profile_id = @real_profile_id,
  8398.                 @parameter_name = '-MetadataRetentionCleanup',
  8399.                 @parameter_value = '1'
  8400.         if (@retcode = 1 or @@ERROR <> 0)
  8401.             goto FAILURE
  8402.  
  8403.         end
  8404.  
  8405.     /* 
  8406.     ** Merge agent : Non default profile for disconnected scenarios ( unreliable link ) 
  8407.     */
  8408.     else if (@profile_id = 7)
  8409.         begin
  8410.  
  8411.         exec @retcode = dbo.sp_add_agent_parameter 
  8412.                 @profile_id = @real_profile_id,
  8413.                 @parameter_name = '-PollingInterval',
  8414.                 @parameter_value = '60'
  8415.         if (@retcode = 1 or @@ERROR <> 0)
  8416.             goto FAILURE
  8417.  
  8418.         exec @retcode = dbo.sp_add_agent_parameter 
  8419.                 @profile_id = @real_profile_id,
  8420.                 @parameter_name = '-StartQueueTimeout',
  8421.                 @parameter_value = '0'
  8422.         if (@retcode = 1 or @@ERROR <> 0)
  8423.             goto FAILURE
  8424.  
  8425.         exec @retcode = dbo.sp_add_agent_parameter 
  8426.                 @profile_id = @real_profile_id,
  8427.                 @parameter_name = '-MaxDownloadChanges',
  8428.                 @parameter_value = '0'
  8429.         if (@retcode = 1 or @@ERROR <> 0)
  8430.             goto FAILURE
  8431.  
  8432.         exec @retcode = dbo.sp_add_agent_parameter 
  8433.                 @profile_id = @real_profile_id,
  8434.                 @parameter_name = '-MaxUploadChanges',
  8435.                 @parameter_value = '0'
  8436.         if (@retcode = 1 or @@ERROR <> 0)
  8437.             goto FAILURE
  8438.             
  8439.         exec @retcode = dbo.sp_add_agent_parameter 
  8440.                 @profile_id = @real_profile_id,
  8441.                 @parameter_name = '-ValidateInterval',
  8442.                 @parameter_value = '60'
  8443.         if (@retcode = 1 or @@ERROR <> 0)
  8444.             goto FAILURE
  8445.  
  8446.         exec @retcode = dbo.sp_add_agent_parameter 
  8447.                 @profile_id = @real_profile_id,
  8448.                 @parameter_name = '-LoginTimeout',
  8449.                 @parameter_value = '15'
  8450.         if (@retcode = 1 or @@ERROR <> 0)
  8451.             goto FAILURE
  8452.  
  8453.         exec @retcode = dbo.sp_add_agent_parameter 
  8454.                 @profile_id = @real_profile_id,
  8455.                 @parameter_name = '-QueryTimeout',
  8456.                 @parameter_value = '300'
  8457.         if (@retcode = 1 or @@ERROR <> 0)
  8458.             goto FAILURE
  8459.  
  8460.         exec @retcode = dbo.sp_add_agent_parameter 
  8461.                 @profile_id = @real_profile_id,
  8462.                 @parameter_name = '-UploadGenerationsPerBatch',
  8463.                 @parameter_value = '20'
  8464.         if (@retcode = 1 or @@ERROR <> 0)
  8465.             goto FAILURE
  8466.  
  8467.         exec @retcode = dbo.sp_add_agent_parameter 
  8468.                 @profile_id = @real_profile_id,
  8469.                 @parameter_name = '-DownloadGenerationsPerBatch',
  8470.                 @parameter_value = '100'
  8471.         if (@retcode = 1 or @@ERROR <> 0)
  8472.             goto FAILURE
  8473.  
  8474.         exec @retcode = dbo.sp_add_agent_parameter 
  8475.                 @profile_id = @real_profile_id,
  8476.                 @parameter_name = '-UploadReadChangesPerBatch',
  8477.                 @parameter_value = '100'
  8478.         if (@retcode = 1 or @@ERROR <> 0)
  8479.             goto FAILURE
  8480.  
  8481.         exec @retcode = dbo.sp_add_agent_parameter 
  8482.                 @profile_id = @real_profile_id,
  8483.                 @parameter_name = '-DownloadReadChangesPerBatch',
  8484.                 @parameter_value = '100'
  8485.         if (@retcode = 1 or @@ERROR <> 0)
  8486.             goto FAILURE
  8487.  
  8488.         exec @retcode = dbo.sp_add_agent_parameter 
  8489.                 @profile_id = @real_profile_id,
  8490.                 @parameter_name = '-UploadWriteChangesPerBatch',
  8491.                 @parameter_value = '100'
  8492.         if (@retcode = 1 or @@ERROR <> 0)
  8493.             goto FAILURE
  8494.  
  8495.         exec @retcode = dbo.sp_add_agent_parameter 
  8496.                 @profile_id = @real_profile_id,
  8497.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  8498.                 @parameter_value = '100'
  8499.         if (@retcode = 1 or @@ERROR <> 0)
  8500.             goto FAILURE
  8501.  
  8502.         exec @retcode = dbo.sp_add_agent_parameter 
  8503.                 @profile_id = @real_profile_id,
  8504.                 @parameter_name = '-Validate',
  8505.                 @parameter_value = '0'
  8506.         if (@retcode = 1 or @@ERROR <> 0)
  8507.             goto FAILURE
  8508.         
  8509.         exec @retcode = dbo.sp_add_agent_parameter 
  8510.                 @profile_id = @real_profile_id,
  8511.                 @parameter_name = '-FastRowCount',
  8512.                 @parameter_value = '1'
  8513.         if (@retcode = 1 or @@ERROR <> 0)
  8514.             goto FAILURE
  8515.  
  8516.         exec @retcode = dbo.sp_add_agent_parameter 
  8517.                 @profile_id = @real_profile_id,
  8518.                 @parameter_name = '-HistoryVerboseLevel',
  8519.                 @parameter_value = '1'
  8520.         if (@retcode = 1 or @@ERROR <> 0)
  8521.             goto FAILURE
  8522.  
  8523.         exec @retcode = dbo.sp_add_agent_parameter 
  8524.                 @profile_id = @real_profile_id,
  8525.                 @parameter_name = '-ChangesPerHistory',
  8526.                 @parameter_value = '100'
  8527.         if (@retcode = 1 or @@ERROR <> 0)
  8528.             goto FAILURE
  8529.  
  8530.         exec @retcode = dbo.sp_add_agent_parameter 
  8531.                 @profile_id = @real_profile_id,
  8532.                 @parameter_name = '-BcpBatchSize',
  8533.                 @parameter_value = '100000'
  8534.         if (@retcode = 1 or @@ERROR <> 0)
  8535.             goto FAILURE
  8536.  
  8537.         exec @retcode = dbo.sp_add_agent_parameter 
  8538.                 @profile_id = @real_profile_id,
  8539.                 @parameter_name = '-KeepAliveMessageInterval',
  8540.                 @parameter_value = '300'
  8541.         if (@retcode = 1 or @@ERROR <> 0)
  8542.             goto FAILURE
  8543.  
  8544.         exec @retcode = dbo.sp_add_agent_parameter 
  8545.                 @profile_id = @real_profile_id,
  8546.                 @parameter_name = '-NumDeadlockRetries',
  8547.                 @parameter_value = '5'
  8548.         if (@retcode = 1 or @@ERROR <> 0)
  8549.             goto FAILURE
  8550.  
  8551.         exec @retcode = dbo.sp_add_agent_parameter 
  8552.                 @profile_id = @real_profile_id,
  8553.                 @parameter_name = '-SrcThreads',
  8554.                 @parameter_value = '3'
  8555.         if (@retcode = 1 or @@ERROR <> 0)
  8556.             goto FAILURE
  8557.  
  8558.         exec @retcode = dbo.sp_add_agent_parameter 
  8559.                 @profile_id = @real_profile_id,
  8560.                 @parameter_name = '-DestThreads',
  8561.                 @parameter_value = '4'
  8562.         if (@retcode = 1 or @@ERROR <> 0)
  8563.             goto FAILURE
  8564.  
  8565.         exec @retcode = dbo.sp_add_agent_parameter
  8566.                 @profile_id = @real_profile_id,
  8567.                 @parameter_name = '-MetadataRetentionCleanup',
  8568.                 @parameter_value = '1'
  8569.         if (@retcode = 1 or @@ERROR <> 0)
  8570.             goto FAILURE
  8571.  
  8572.         end
  8573.  
  8574.         /* 
  8575.     ** Merge agent : Default profile for well connected scenarios - Verbose history logging 
  8576.     */
  8577.     else if (@profile_id = 8)
  8578.         begin
  8579.         exec @retcode = dbo.sp_add_agent_parameter 
  8580.                 @profile_id = @real_profile_id,
  8581.                 @parameter_name = '-PollingInterval',
  8582.                 @parameter_value = '60'
  8583.         if (@retcode = 1 or @@ERROR <> 0)
  8584.             goto FAILURE
  8585.             
  8586.         exec @retcode = dbo.sp_add_agent_parameter 
  8587.                 @profile_id = @real_profile_id,
  8588.                 @parameter_name = '-StartQueueTimeout',
  8589.                 @parameter_value = '0'
  8590.         if (@retcode = 1 or @@ERROR <> 0)
  8591.             goto FAILURE
  8592.             
  8593.         exec @retcode = dbo.sp_add_agent_parameter 
  8594.                 @profile_id = @real_profile_id,
  8595.                 @parameter_name = '-MaxDownloadChanges',
  8596.                 @parameter_value = '0'
  8597.         if (@retcode = 1 or @@ERROR <> 0)
  8598.             goto FAILURE
  8599.  
  8600.         exec @retcode = dbo.sp_add_agent_parameter 
  8601.                 @profile_id = @real_profile_id,
  8602.                 @parameter_name = '-MaxUploadChanges',
  8603.                 @parameter_value = '0'
  8604.         if (@retcode = 1 or @@ERROR <> 0)
  8605.             goto FAILURE
  8606.  
  8607.         exec @retcode = dbo.sp_add_agent_parameter 
  8608.                 @profile_id = @real_profile_id,
  8609.                 @parameter_name = '-ValidateInterval',
  8610.                 @parameter_value = '60'
  8611.         if (@retcode = 1 or @@ERROR <> 0)
  8612.             goto FAILURE
  8613.  
  8614.         exec @retcode = dbo.sp_add_agent_parameter 
  8615.                 @profile_id = @real_profile_id,
  8616.                 @parameter_name = '-LoginTimeout',
  8617.                 @parameter_value = '15'
  8618.         if (@retcode = 1 or @@ERROR <> 0)
  8619.             goto FAILURE
  8620.  
  8621.         exec @retcode = dbo.sp_add_agent_parameter 
  8622.                 @profile_id = @real_profile_id,
  8623.                 @parameter_name = '-QueryTimeout',
  8624.                 @parameter_value = '300'        
  8625.         if (@retcode = 1 or @@ERROR <> 0)
  8626.             goto FAILURE
  8627.  
  8628.         exec @retcode = dbo.sp_add_agent_parameter 
  8629.                 @profile_id = @real_profile_id,
  8630.                 @parameter_name = '-UploadGenerationsPerBatch',
  8631.                 @parameter_value = '100'
  8632.         if (@retcode = 1 or @@ERROR <> 0)
  8633.             goto FAILURE
  8634.  
  8635.         exec @retcode = dbo.sp_add_agent_parameter 
  8636.                 @profile_id = @real_profile_id,
  8637.                 @parameter_name = '-DownloadGenerationsPerBatch',
  8638.                 @parameter_value = '100'
  8639.         if (@retcode = 1 or @@ERROR <> 0)
  8640.             goto FAILURE
  8641.  
  8642.         exec @retcode = dbo.sp_add_agent_parameter 
  8643.                 @profile_id = @real_profile_id,
  8644.                 @parameter_name = '-UploadReadChangesPerBatch',
  8645.                 @parameter_value = '100'
  8646.         if (@retcode = 1 or @@ERROR <> 0)
  8647.             goto FAILURE
  8648.  
  8649.         exec @retcode = dbo.sp_add_agent_parameter 
  8650.                 @profile_id = @real_profile_id,
  8651.                 @parameter_name = '-DownloadReadChangesPerBatch',
  8652.                 @parameter_value = '100'
  8653.         if (@retcode = 1 or @@ERROR <> 0)
  8654.             goto FAILURE
  8655.  
  8656.         exec @retcode = dbo.sp_add_agent_parameter 
  8657.                 @profile_id = @real_profile_id,
  8658.                 @parameter_name = '-UploadWriteChangesPerBatch',
  8659.                 @parameter_value = '100'
  8660.         if (@retcode = 1 or @@ERROR <> 0)
  8661.             goto FAILURE
  8662.  
  8663.         exec @retcode = dbo.sp_add_agent_parameter 
  8664.                 @profile_id = @real_profile_id,
  8665.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  8666.                 @parameter_value = '100'
  8667.         if (@retcode = 1 or @@ERROR <> 0)
  8668.             goto FAILURE
  8669.  
  8670.         exec @retcode = dbo.sp_add_agent_parameter 
  8671.                 @profile_id = @real_profile_id,
  8672.                 @parameter_name = '-Validate',
  8673.                 @parameter_value = '0'
  8674.         if (@retcode = 1 or @@ERROR <> 0)
  8675.             goto FAILURE
  8676.  
  8677.         exec @retcode = dbo.sp_add_agent_parameter 
  8678.                 @profile_id = @real_profile_id,
  8679.                 @parameter_name = '-FastRowCount',
  8680.                 @parameter_value = '1'
  8681.         if (@retcode = 1 or @@ERROR <> 0)
  8682.             goto FAILURE
  8683.         
  8684.         exec @retcode = dbo.sp_add_agent_parameter 
  8685.                 @profile_id = @real_profile_id,
  8686.                 @parameter_name = '-HistoryVerboseLevel',
  8687.                 @parameter_value = '3'
  8688.         if (@retcode = 1 or @@ERROR <> 0)
  8689.             goto FAILURE
  8690.  
  8691.         exec @retcode = dbo.sp_add_agent_parameter 
  8692.                 @profile_id = @real_profile_id,
  8693.                 @parameter_name = '-ChangesPerHistory',
  8694.                 @parameter_value = '50'
  8695.         if (@retcode = 1 or @@ERROR <> 0)
  8696.             goto FAILURE
  8697.  
  8698.         exec @retcode = dbo.sp_add_agent_parameter 
  8699.                 @profile_id = @real_profile_id,
  8700.                 @parameter_name = '-BcpBatchSize',
  8701.                 @parameter_value = '100000'
  8702.         if (@retcode = 1 or @@ERROR <> 0)
  8703.             goto FAILURE
  8704.  
  8705.         exec @retcode = dbo.sp_add_agent_parameter 
  8706.                 @profile_id = @real_profile_id,
  8707.                 @parameter_name = '-NumDeadlockRetries',
  8708.                 @parameter_value = '5'
  8709.         if (@retcode = 1 or @@ERROR <> 0)
  8710.             goto FAILURE
  8711.  
  8712.         exec @retcode = dbo.sp_add_agent_parameter 
  8713.                 @profile_id = @real_profile_id,
  8714.                 @parameter_name = '-SrcThreads',
  8715.                 @parameter_value = '3'
  8716.         if (@retcode = 1 or @@ERROR <> 0)
  8717.             goto FAILURE
  8718.  
  8719.         exec @retcode = dbo.sp_add_agent_parameter 
  8720.                 @profile_id = @real_profile_id,
  8721.                 @parameter_name = '-DestThreads',
  8722.                 @parameter_value = '4'
  8723.         if (@retcode = 1 or @@ERROR <> 0)
  8724.             goto FAILURE
  8725.  
  8726.         exec @retcode = dbo.sp_add_agent_parameter
  8727.                 @profile_id = @real_profile_id,
  8728.                 @parameter_name = '-KeepAliveMessageInterval',
  8729.                 @parameter_value = '300'
  8730.         if (@retcode = 1 or @@ERROR <> 0)
  8731.             goto FAILURE
  8732.  
  8733.         exec @retcode = dbo.sp_add_agent_parameter
  8734.                 @profile_id = @real_profile_id,
  8735.                 @parameter_name = '-MetadataRetentionCleanup',
  8736.                 @parameter_value = '1'
  8737.         if (@retcode = 1 or @@ERROR <> 0)
  8738.             goto FAILURE
  8739.  
  8740.         end
  8741.  
  8742.     /* 
  8743.     ** Merge agent : (Synchronization Manager Profile)
  8744.     */
  8745.     else if (@profile_id = 9)
  8746.         begin
  8747.         exec @retcode = dbo.sp_add_agent_parameter 
  8748.                 @profile_id = @real_profile_id,
  8749.                 @parameter_name = '-PollingInterval',
  8750.                 @parameter_value = '60'
  8751.         if (@retcode = 1 or @@ERROR <> 0)
  8752.             goto FAILURE
  8753.  
  8754.         exec @retcode = dbo.sp_add_agent_parameter 
  8755.                 @profile_id = @real_profile_id,
  8756.                 @parameter_name = '-StartQueueTimeout',
  8757.                 @parameter_value = '0'
  8758.         if (@retcode = 1 or @@ERROR <> 0)
  8759.             goto FAILURE
  8760.  
  8761.         exec @retcode = dbo.sp_add_agent_parameter 
  8762.                 @profile_id = @real_profile_id,
  8763.                 @parameter_name = '-MaxDownloadChanges',
  8764.                 @parameter_value = '0'
  8765.         if (@retcode = 1 or @@ERROR <> 0)
  8766.             goto FAILURE
  8767.  
  8768.         exec @retcode = dbo.sp_add_agent_parameter 
  8769.                 @profile_id = @real_profile_id,
  8770.                 @parameter_name = '-MaxUploadChanges',
  8771.                 @parameter_value = '0'
  8772.         if (@retcode = 1 or @@ERROR <> 0)
  8773.             goto FAILURE
  8774.             
  8775.         exec @retcode = dbo.sp_add_agent_parameter 
  8776.                 @profile_id = @real_profile_id,
  8777.                 @parameter_name = '-ValidateInterval',
  8778.                 @parameter_value = '60'
  8779.         if (@retcode = 1 or @@ERROR <> 0)
  8780.             goto FAILURE
  8781.  
  8782.         exec @retcode = dbo.sp_add_agent_parameter 
  8783.                 @profile_id = @real_profile_id,
  8784.                 @parameter_name = '-LoginTimeout',
  8785.                 @parameter_value = '15'
  8786.         if (@retcode = 1 or @@ERROR <> 0)
  8787.             goto FAILURE
  8788.  
  8789.         exec @retcode = dbo.sp_add_agent_parameter 
  8790.                 @profile_id = @real_profile_id,
  8791.                 @parameter_name = '-QueryTimeout',
  8792.                 @parameter_value = '300'
  8793.         if (@retcode = 1 or @@ERROR <> 0)
  8794.             goto FAILURE
  8795.  
  8796.         exec @retcode = dbo.sp_add_agent_parameter 
  8797.                 @profile_id = @real_profile_id,
  8798.                 @parameter_name = '-UploadGenerationsPerBatch',
  8799.                 @parameter_value = '100'
  8800.         if (@retcode = 1 or @@ERROR <> 0)
  8801.             goto FAILURE
  8802.  
  8803.         exec @retcode = dbo.sp_add_agent_parameter 
  8804.                 @profile_id = @real_profile_id,
  8805.                 @parameter_name = '-DownloadGenerationsPerBatch',
  8806.                 @parameter_value = '100'
  8807.         if (@retcode = 1 or @@ERROR <> 0)
  8808.             goto FAILURE
  8809.  
  8810.         exec @retcode = dbo.sp_add_agent_parameter 
  8811.                 @profile_id = @real_profile_id,
  8812.                 @parameter_name = '-UploadReadChangesPerBatch',
  8813.                 @parameter_value = '100'
  8814.         if (@retcode = 1 or @@ERROR <> 0)
  8815.             goto FAILURE
  8816.  
  8817.         exec @retcode = dbo.sp_add_agent_parameter 
  8818.                 @profile_id = @real_profile_id,
  8819.                 @parameter_name = '-DownloadReadChangesPerBatch',
  8820.                 @parameter_value = '100'
  8821.         if (@retcode = 1 or @@ERROR <> 0)
  8822.             goto FAILURE
  8823.  
  8824.         exec @retcode = dbo.sp_add_agent_parameter 
  8825.                 @profile_id = @real_profile_id,
  8826.                 @parameter_name = '-UploadWriteChangesPerBatch',
  8827.                 @parameter_value = '100'
  8828.         if (@retcode = 1 or @@ERROR <> 0)
  8829.             goto FAILURE
  8830.  
  8831.         exec @retcode = dbo.sp_add_agent_parameter 
  8832.                 @profile_id = @real_profile_id,
  8833.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  8834.                 @parameter_value = '100'
  8835.         if (@retcode = 1 or @@ERROR <> 0)
  8836.             goto FAILURE
  8837.  
  8838.         exec @retcode = dbo.sp_add_agent_parameter 
  8839.                 @profile_id = @real_profile_id,
  8840.                 @parameter_name = '-Validate',
  8841.                 @parameter_value = '0'
  8842.         if (@retcode = 1 or @@ERROR <> 0)
  8843.             goto FAILURE
  8844.         
  8845.         exec @retcode = dbo.sp_add_agent_parameter 
  8846.                 @profile_id = @real_profile_id,
  8847.                 @parameter_name = '-FastRowCount',
  8848.                 @parameter_value = '1'
  8849.         if (@retcode = 1 or @@ERROR <> 0)
  8850.             goto FAILURE
  8851.  
  8852.         exec @retcode = dbo.sp_add_agent_parameter 
  8853.                 @profile_id = @real_profile_id,
  8854.                 @parameter_name = '-HistoryVerboseLevel',
  8855.                 @parameter_value = '1'
  8856.         if (@retcode = 1 or @@ERROR <> 0)
  8857.             goto FAILURE
  8858.  
  8859.         exec @retcode = dbo.sp_add_agent_parameter 
  8860.                 @profile_id = @real_profile_id,
  8861.                 @parameter_name = '-ChangesPerHistory',
  8862.                 @parameter_value = '50'
  8863.         if (@retcode = 1 or @@ERROR <> 0)
  8864.             goto FAILURE
  8865.  
  8866.         exec @retcode = dbo.sp_add_agent_parameter 
  8867.                 @profile_id = @real_profile_id,
  8868.                 @parameter_name = '-BcpBatchSize',
  8869.                 @parameter_value = '1000'
  8870.         if (@retcode = 1 or @@ERROR <> 0)
  8871.             goto FAILURE
  8872.  
  8873.         exec @retcode = dbo.sp_add_agent_parameter 
  8874.                 @profile_id = @real_profile_id,
  8875.                 @parameter_name = '-NumDeadlockRetries',
  8876.                 @parameter_value = '5'
  8877.         if (@retcode = 1 or @@ERROR <> 0)
  8878.             goto FAILURE
  8879.  
  8880.         exec @retcode = dbo.sp_add_agent_parameter 
  8881.                 @profile_id = @real_profile_id,
  8882.                 @parameter_name = '-SrcThreads',
  8883.                 @parameter_value = '3'
  8884.         if (@retcode = 1 or @@ERROR <> 0)
  8885.             goto FAILURE
  8886.  
  8887.         exec @retcode = dbo.sp_add_agent_parameter 
  8888.                 @profile_id = @real_profile_id,
  8889.                 @parameter_name = '-DestThreads',
  8890.                 @parameter_value = '4'
  8891.         if (@retcode = 1 or @@ERROR <> 0)
  8892.             goto FAILURE
  8893.  
  8894.         exec @retcode = dbo.sp_add_agent_parameter
  8895.                 @profile_id = @real_profile_id,
  8896.                 @parameter_name = '-KeepAliveMessageInterval',
  8897.                 @parameter_value = '300'
  8898.         if (@retcode = 1 or @@ERROR <> 0)
  8899.             goto FAILURE
  8900.  
  8901.         exec @retcode = dbo.sp_add_agent_parameter
  8902.                 @profile_id = @real_profile_id,
  8903.                 @parameter_name = '-MetadataRetentionCleanup',
  8904.                 @parameter_value = '1'
  8905.         if (@retcode = 1 or @@ERROR <> 0)
  8906.             goto FAILURE
  8907.  
  8908.         end
  8909.  
  8910.     /* 
  8911.     ** Distribution agent (Synchronization Manager Profile)
  8912.     */
  8913.     else if (@profile_id = 10)
  8914.         begin
  8915.         exec @retcode = dbo.sp_add_agent_parameter 
  8916.                 @profile_id = @real_profile_id,
  8917.                 @parameter_name = '-BcpBatchSize',
  8918.                 @parameter_value = '1000'
  8919.         if (@retcode = 1 or @@ERROR <> 0)
  8920.             goto FAILURE
  8921.  
  8922.         exec @retcode = dbo.sp_add_agent_parameter 
  8923.                 @profile_id = @real_profile_id,
  8924.                 @parameter_name = '-CommitBatchSize',
  8925.                 @parameter_value = '100'
  8926.         if (@retcode = 1 or @@ERROR <> 0)
  8927.             goto FAILURE
  8928.  
  8929.         exec @retcode = dbo.sp_add_agent_parameter 
  8930.                 @profile_id = @real_profile_id,
  8931.                 @parameter_name = '-CommitBatchThreshold',
  8932.                 @parameter_value = '1000'
  8933.         if (@retcode = 1 or @@ERROR <> 0)
  8934.             goto FAILURE
  8935.  
  8936.         exec @retcode = dbo.sp_add_agent_parameter 
  8937.                 @profile_id = @real_profile_id,
  8938.                 @parameter_name = '-HistoryVerboseLevel',
  8939.                 @parameter_value = '1'
  8940.         if (@retcode = 1 or @@ERROR <> 0)
  8941.             goto FAILURE
  8942.  
  8943.         exec @retcode = dbo.sp_add_agent_parameter 
  8944.                 @profile_id = @real_profile_id,
  8945.                 @parameter_name = '-LoginTimeout',
  8946.                 @parameter_value = '15'
  8947.         if (@retcode = 1 or @@ERROR <> 0)
  8948.             goto FAILURE
  8949.  
  8950.         exec @retcode = dbo.sp_add_agent_parameter 
  8951.                 @profile_id = @real_profile_id,
  8952.                 @parameter_name = '-MaxBcpThreads',
  8953.                 @parameter_value = '1'
  8954.         if (@retcode = 1 or @@ERROR <> 0)
  8955.             goto FAILURE
  8956.  
  8957.         exec @retcode = dbo.sp_add_agent_parameter 
  8958.                 @profile_id = @real_profile_id,
  8959.                 @parameter_name = '-MaxDeliveredTransactions',
  8960.                 @parameter_value = '0'
  8961.         if (@retcode = 1 or @@ERROR <> 0)
  8962.             goto FAILURE
  8963.  
  8964.         exec @retcode = dbo.sp_add_agent_parameter 
  8965.                 @profile_id = @real_profile_id,
  8966.                 @parameter_name = '-PollingInterval',
  8967.                 @parameter_value = '10'
  8968.         if (@retcode = 1 or @@ERROR <> 0)
  8969.             goto FAILURE
  8970.  
  8971.         exec @retcode = dbo.sp_add_agent_parameter 
  8972.                 @profile_id = @real_profile_id,
  8973.                 @parameter_name = '-QueryTimeout',
  8974.                 @parameter_value = '300'        -- 5 minutes 
  8975.         if (@retcode = 1 or @@ERROR <> 0)
  8976.             goto FAILURE
  8977.  
  8978.         exec @retcode = dbo.sp_add_agent_parameter 
  8979.                 @profile_id = @real_profile_id,
  8980.                 @parameter_name = '-TransactionsPerHistory',
  8981.                 @parameter_value = '100'
  8982.         if (@retcode = 1 or @@ERROR <> 0)
  8983.             goto FAILURE
  8984.  
  8985.         exec @retcode = dbo.sp_add_agent_parameter 
  8986.                 @profile_id = @real_profile_id,
  8987.                 @parameter_name = '-SkipErrors',
  8988.                 @parameter_value = ''
  8989.         if (@retcode = 1 or @@ERROR <> 0)
  8990.             goto FAILURE
  8991.  
  8992.         exec @retcode = dbo.sp_add_agent_parameter
  8993.                 @profile_id = @real_profile_id,
  8994.                 @parameter_name = '-KeepAliveMessageInterval',
  8995.                 @parameter_value = '300'
  8996.         if (@retcode = 1 or @@ERROR <> 0)
  8997.             goto FAILURE
  8998.  
  8999.         end
  9000.     
  9001.     /* 
  9002.     ** Queue Reader agent default profile
  9003.     */
  9004.     else if (@profile_id = 11)
  9005.     begin
  9006.         exec @retcode = dbo.sp_add_agent_parameter 
  9007.                 @profile_id = @real_profile_id,
  9008.                 @parameter_name = '-HistoryVerboseLevel',
  9009.                 @parameter_value = '1'
  9010.         if (@retcode = 1 or @@ERROR <> 0)
  9011.             goto FAILURE
  9012.  
  9013.         exec @retcode = dbo.sp_add_agent_parameter 
  9014.                 @profile_id = @real_profile_id,
  9015.                 @parameter_name = '-PollingInterval',
  9016.                 @parameter_value = '10'
  9017.         if (@retcode = 1 or @@ERROR <> 0)
  9018.             goto FAILURE
  9019.  
  9020.         exec @retcode = dbo.sp_add_agent_parameter 
  9021.                 @profile_id = @real_profile_id,
  9022.                 @parameter_name = '-LoginTimeout',
  9023.                 @parameter_value = '15'
  9024.         if (@retcode = 1 or @@ERROR <> 0)
  9025.             goto FAILURE
  9026.  
  9027.         exec @retcode = dbo.sp_add_agent_parameter 
  9028.                 @profile_id = @real_profile_id,
  9029.                 @parameter_name = '-QueryTimeout',
  9030.                 @parameter_value = '300'        -- 5 minutes 
  9031.         if (@retcode = 1 or @@ERROR <> 0)
  9032.             goto FAILURE
  9033.     end
  9034.       
  9035.     /* 
  9036.     ** Merge agent : Rowcount Validation profile for well connected scenarios 
  9037.     */
  9038.     else if (@profile_id = 12)
  9039.         begin
  9040.         exec @retcode = dbo.sp_add_agent_parameter 
  9041.                 @profile_id = @real_profile_id,
  9042.                 @parameter_name = '-PollingInterval',
  9043.                 @parameter_value = '60'
  9044.         if (@retcode = 1 or @@ERROR <> 0)
  9045.             goto FAILURE
  9046.  
  9047.  
  9048.         exec @retcode = dbo.sp_add_agent_parameter 
  9049.                 @profile_id = @real_profile_id,
  9050.                 @parameter_name = '-StartQueueTimeout',
  9051.                 @parameter_value = '0'
  9052.         if (@retcode = 1 or @@ERROR <> 0)
  9053.             goto FAILURE
  9054.  
  9055.         exec @retcode = dbo.sp_add_agent_parameter 
  9056.                 @profile_id = @real_profile_id,
  9057.                 @parameter_name = '-MaxDownloadChanges',
  9058.                 @parameter_value = '0'
  9059.         if (@retcode = 1 or @@ERROR <> 0)
  9060.             goto FAILURE
  9061.  
  9062.         exec @retcode = dbo.sp_add_agent_parameter 
  9063.                 @profile_id = @real_profile_id,
  9064.                 @parameter_name = '-MaxUploadChanges',
  9065.                 @parameter_value = '0'
  9066.         if (@retcode = 1 or @@ERROR <> 0)
  9067.             goto FAILURE
  9068.  
  9069.         exec @retcode = dbo.sp_add_agent_parameter 
  9070.                 @profile_id = @real_profile_id,
  9071.                 @parameter_name = '-ValidateInterval',
  9072.                 @parameter_value = '60'
  9073.         if (@retcode = 1 or @@ERROR <> 0)
  9074.             goto FAILURE
  9075.  
  9076.         exec @retcode = dbo.sp_add_agent_parameter 
  9077.                 @profile_id = @real_profile_id,
  9078.                 @parameter_name = '-LoginTimeout',
  9079.                 @parameter_value = '15'
  9080.         if (@retcode = 1 or @@ERROR <> 0)
  9081.             goto FAILURE
  9082.  
  9083.         exec @retcode = dbo.sp_add_agent_parameter 
  9084.                 @profile_id = @real_profile_id,
  9085.                 @parameter_name = '-QueryTimeout',
  9086.                 @parameter_value = '300'        
  9087.         if (@retcode = 1 or @@ERROR <> 0)
  9088.             goto FAILURE
  9089.  
  9090.         exec @retcode = dbo.sp_add_agent_parameter 
  9091.                 @profile_id = @real_profile_id,
  9092.                 @parameter_name = '-UploadGenerationsPerBatch',
  9093.                 @parameter_value = '100'
  9094.         if (@retcode = 1 or @@ERROR <> 0)
  9095.             goto FAILURE
  9096.  
  9097.         exec @retcode = dbo.sp_add_agent_parameter 
  9098.                 @profile_id = @real_profile_id,
  9099.                 @parameter_name = '-DownloadGenerationsPerBatch',
  9100.                 @parameter_value = '100'
  9101.         if (@retcode = 1 or @@ERROR <> 0)
  9102.             goto FAILURE
  9103.  
  9104.         exec @retcode = dbo.sp_add_agent_parameter 
  9105.                 @profile_id = @real_profile_id,
  9106.                 @parameter_name = '-UploadReadChangesPerBatch',
  9107.                 @parameter_value = '100'
  9108.         if (@retcode = 1 or @@ERROR <> 0)
  9109.             goto FAILURE
  9110.  
  9111.         exec @retcode = dbo.sp_add_agent_parameter 
  9112.                 @profile_id = @real_profile_id,
  9113.                 @parameter_name = '-DownloadReadChangesPerBatch',
  9114.                 @parameter_value = '100'
  9115.         if (@retcode = 1 or @@ERROR <> 0)
  9116.             goto FAILURE
  9117.  
  9118.         exec @retcode = dbo.sp_add_agent_parameter 
  9119.                 @profile_id = @real_profile_id,
  9120.                 @parameter_name = '-UploadWriteChangesPerBatch',
  9121.                 @parameter_value = '100'
  9122.         if (@retcode = 1 or @@ERROR <> 0)
  9123.             goto FAILURE
  9124.  
  9125.         exec @retcode = dbo.sp_add_agent_parameter 
  9126.                 @profile_id = @real_profile_id,
  9127.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  9128.                 @parameter_value = '100'
  9129.         if (@retcode = 1 or @@ERROR <> 0)
  9130.             goto FAILURE
  9131.  
  9132.         exec @retcode = dbo.sp_add_agent_parameter 
  9133.                 @profile_id = @real_profile_id,
  9134.                 @parameter_name = '-Validate',
  9135.                 @parameter_value = '1'
  9136.         if (@retcode = 1 or @@ERROR <> 0)
  9137.             goto FAILURE
  9138.  
  9139.         exec @retcode = dbo.sp_add_agent_parameter 
  9140.                 @profile_id = @real_profile_id,
  9141.                 @parameter_name = '-FastRowCount',
  9142.                 @parameter_value = '1'
  9143.         if (@retcode = 1 or @@ERROR <> 0)
  9144.             goto FAILURE
  9145.         
  9146.         exec @retcode = dbo.sp_add_agent_parameter 
  9147.                 @profile_id = @real_profile_id,
  9148.                 @parameter_name = '-HistoryVerboseLevel',
  9149.                 @parameter_value = '1'
  9150.         if (@retcode = 1 or @@ERROR <> 0)
  9151.             goto FAILURE
  9152.  
  9153.         exec @retcode = dbo.sp_add_agent_parameter 
  9154.                 @profile_id = @real_profile_id,
  9155.                 @parameter_name = '-ChangesPerHistory',
  9156.                 @parameter_value = '100'
  9157.         if (@retcode = 1 or @@ERROR <> 0)
  9158.             goto FAILURE
  9159.         exec @retcode = dbo.sp_add_agent_parameter 
  9160.                 @profile_id = @real_profile_id,
  9161.                 @parameter_name = '-BcpBatchSize',
  9162.                 @parameter_value = '100000'
  9163.         if (@retcode = 1 or @@ERROR <> 0)
  9164.             goto FAILURE
  9165.  
  9166.         exec @retcode = dbo.sp_add_agent_parameter 
  9167.                 @profile_id = @real_profile_id,
  9168.                 @parameter_name = '-NumDeadlockRetries',
  9169.                 @parameter_value = '5'
  9170.         if (@retcode = 1 or @@ERROR <> 0)
  9171.             goto FAILURE
  9172.  
  9173.         exec @retcode = dbo.sp_add_agent_parameter 
  9174.                 @profile_id = @real_profile_id,
  9175.                 @parameter_name = '-SrcThreads',
  9176.                 @parameter_value = '3'
  9177.         if (@retcode = 1 or @@ERROR <> 0)
  9178.             goto FAILURE
  9179.  
  9180.         exec @retcode = dbo.sp_add_agent_parameter 
  9181.                 @profile_id = @real_profile_id,
  9182.                 @parameter_name = '-DestThreads',
  9183.                 @parameter_value = '4'
  9184.         if (@retcode = 1 or @@ERROR <> 0)
  9185.             goto FAILURE
  9186.             
  9187.         exec @retcode = dbo.sp_add_agent_parameter
  9188.                 @profile_id = @real_profile_id,
  9189.                 @parameter_name = '-KeepAliveMessageInterval',
  9190.                 @parameter_value = '300'
  9191.         if (@retcode = 1 or @@ERROR <> 0)
  9192.             goto FAILURE
  9193.  
  9194.         exec @retcode = dbo.sp_add_agent_parameter
  9195.                 @profile_id = @real_profile_id,
  9196.                 @parameter_name = '-MetadataRetentionCleanup',
  9197.                 @parameter_value = '1'
  9198.         if (@retcode = 1 or @@ERROR <> 0)
  9199.             goto FAILURE
  9200.  
  9201.         end
  9202.  
  9203.     /* 
  9204.     ** Merge agent : Rowcount & Checksum Validation profile for well connected scenarios 
  9205.     */
  9206.     else if (@profile_id = 13)
  9207.         begin
  9208.         exec @retcode = dbo.sp_add_agent_parameter 
  9209.                 @profile_id = @real_profile_id,
  9210.                 @parameter_name = '-PollingInterval',
  9211.                 @parameter_value = '60'
  9212.         if (@retcode = 1 or @@ERROR <> 0)
  9213.             goto FAILURE
  9214.  
  9215.         exec @retcode = dbo.sp_add_agent_parameter 
  9216.                 @profile_id = @real_profile_id,
  9217.                 @parameter_name = '-StartQueueTimeout',
  9218.                 @parameter_value = '0'
  9219.         if (@retcode = 1 or @@ERROR <> 0)
  9220.             goto FAILURE
  9221.  
  9222.         exec @retcode = dbo.sp_add_agent_parameter 
  9223.                 @profile_id = @real_profile_id,
  9224.                 @parameter_name = '-MaxDownloadChanges',
  9225.                 @parameter_value = '0'
  9226.         if (@retcode = 1 or @@ERROR <> 0)
  9227.             goto FAILURE
  9228.  
  9229.         exec @retcode = dbo.sp_add_agent_parameter 
  9230.                 @profile_id = @real_profile_id,
  9231.                 @parameter_name = '-MaxUploadChanges',
  9232.                 @parameter_value = '0'
  9233.         if (@retcode = 1 or @@ERROR <> 0)
  9234.             goto FAILURE
  9235.  
  9236.         exec @retcode = dbo.sp_add_agent_parameter 
  9237.                 @profile_id = @real_profile_id,
  9238.                 @parameter_name = '-ValidateInterval',
  9239.                 @parameter_value = '60'
  9240.         if (@retcode = 1 or @@ERROR <> 0)
  9241.             goto FAILURE
  9242.  
  9243.         exec @retcode = dbo.sp_add_agent_parameter 
  9244.                 @profile_id = @real_profile_id,
  9245.                 @parameter_name = '-LoginTimeout',
  9246.                 @parameter_value = '15'
  9247.         if (@retcode = 1 or @@ERROR <> 0)
  9248.             goto FAILURE
  9249.  
  9250.         exec @retcode = dbo.sp_add_agent_parameter 
  9251.                 @profile_id = @real_profile_id,
  9252.                 @parameter_name = '-QueryTimeout',
  9253.                 @parameter_value = '300'        
  9254.         if (@retcode = 1 or @@ERROR <> 0)
  9255.             goto FAILURE
  9256.  
  9257.         exec @retcode = dbo.sp_add_agent_parameter 
  9258.                 @profile_id = @real_profile_id,
  9259.                 @parameter_name = '-UploadGenerationsPerBatch',
  9260.                 @parameter_value = '100'
  9261.         if (@retcode = 1 or @@ERROR <> 0)
  9262.             goto FAILURE
  9263.  
  9264.         exec @retcode = dbo.sp_add_agent_parameter 
  9265.                 @profile_id = @real_profile_id,
  9266.                 @parameter_name = '-DownloadGenerationsPerBatch',
  9267.                 @parameter_value = '100'
  9268.         if (@retcode = 1 or @@ERROR <> 0)
  9269.             goto FAILURE
  9270.  
  9271.         exec @retcode = dbo.sp_add_agent_parameter 
  9272.                 @profile_id = @real_profile_id,
  9273.                 @parameter_name = '-UploadReadChangesPerBatch',
  9274.                 @parameter_value = '100'
  9275.         if (@retcode = 1 or @@ERROR <> 0)
  9276.             goto FAILURE
  9277.  
  9278.         exec @retcode = dbo.sp_add_agent_parameter 
  9279.                 @profile_id = @real_profile_id,
  9280.                 @parameter_name = '-DownloadReadChangesPerBatch',
  9281.                 @parameter_value = '100'
  9282.         if (@retcode = 1 or @@ERROR <> 0)
  9283.             goto FAILURE
  9284.  
  9285.         exec @retcode = dbo.sp_add_agent_parameter 
  9286.                 @profile_id = @real_profile_id,
  9287.                 @parameter_name = '-UploadWriteChangesPerBatch',
  9288.                 @parameter_value = '100'
  9289.         if (@retcode = 1 or @@ERROR <> 0)
  9290.             goto FAILURE
  9291.  
  9292.         exec @retcode = dbo.sp_add_agent_parameter 
  9293.                 @profile_id = @real_profile_id,
  9294.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  9295.                 @parameter_value = '100'
  9296.         if (@retcode = 1 or @@ERROR <> 0)
  9297.             goto FAILURE
  9298.  
  9299.         exec @retcode = dbo.sp_add_agent_parameter 
  9300.                 @profile_id = @real_profile_id,
  9301.                 @parameter_name = '-Validate',
  9302.                 @parameter_value = '3'
  9303.         if (@retcode = 1 or @@ERROR <> 0)
  9304.             goto FAILURE
  9305.  
  9306.         exec @retcode = dbo.sp_add_agent_parameter 
  9307.                 @profile_id = @real_profile_id,
  9308.                 @parameter_name = '-FastRowCount',
  9309.                 @parameter_value = '1'
  9310.         if (@retcode = 1 or @@ERROR <> 0)
  9311.             goto FAILURE
  9312.         
  9313.         exec @retcode = dbo.sp_add_agent_parameter 
  9314.                 @profile_id = @real_profile_id,
  9315.                 @parameter_name = '-HistoryVerboseLevel',
  9316.                 @parameter_value = '1'
  9317.         if (@retcode = 1 or @@ERROR <> 0)
  9318.             goto FAILURE
  9319.  
  9320.         exec @retcode = dbo.sp_add_agent_parameter 
  9321.                 @profile_id = @real_profile_id,
  9322.                 @parameter_name = '-ChangesPerHistory',
  9323.                 @parameter_value = '100'
  9324.         if (@retcode = 1 or @@ERROR <> 0)
  9325.             goto FAILURE
  9326.         exec @retcode = dbo.sp_add_agent_parameter 
  9327.                 @profile_id = @real_profile_id,
  9328.                 @parameter_name = '-BcpBatchSize',
  9329.                 @parameter_value = '100000'
  9330.         if (@retcode = 1 or @@ERROR <> 0)
  9331.             goto FAILURE
  9332.  
  9333.         exec @retcode = dbo.sp_add_agent_parameter 
  9334.                 @profile_id = @real_profile_id,
  9335.                 @parameter_name = '-NumDeadlockRetries',
  9336.                 @parameter_value = '5'
  9337.         if (@retcode = 1 or @@ERROR <> 0)
  9338.             goto FAILURE
  9339.             
  9340.         exec @retcode = dbo.sp_add_agent_parameter 
  9341.                 @profile_id = @real_profile_id,
  9342.                 @parameter_name = '-SrcThreads',
  9343.                 @parameter_value = '3'
  9344.         if (@retcode = 1 or @@ERROR <> 0)
  9345.             goto FAILURE
  9346.  
  9347.         exec @retcode = dbo.sp_add_agent_parameter 
  9348.                 @profile_id = @real_profile_id,
  9349.                 @parameter_name = '-DestThreads',
  9350.                 @parameter_value = '4'
  9351.         if (@retcode = 1 or @@ERROR <> 0)
  9352.             goto FAILURE
  9353.  
  9354.         exec @retcode = dbo.sp_add_agent_parameter
  9355.                 @profile_id = @real_profile_id,
  9356.                 @parameter_name = '-KeepAliveMessageInterval',
  9357.                 @parameter_value = '300'
  9358.         if (@retcode = 1 or @@ERROR <> 0)
  9359.             goto FAILURE
  9360.  
  9361.         exec @retcode = dbo.sp_add_agent_parameter
  9362.                 @profile_id = @real_profile_id,
  9363.                 @parameter_name = '-MetadataRetentionCleanup',
  9364.                 @parameter_value = '1'
  9365.         if (@retcode = 1 or @@ERROR <> 0)
  9366.             goto FAILURE
  9367.  
  9368.         end
  9369.  
  9370.     /* 
  9371.     ** Distribution agent, skip error profile
  9372.     */
  9373.     else if (@profile_id = 14)
  9374.       begin
  9375.  
  9376.         exec @retcode = dbo.sp_add_agent_parameter 
  9377.                 @profile_id = @real_profile_id,
  9378.                 @parameter_name = '-BcpBatchSize',
  9379.                 @parameter_value = '100000'
  9380.         if (@retcode = 1 or @@ERROR <> 0)
  9381.             goto FAILURE
  9382.  
  9383.         exec @retcode = dbo.sp_add_agent_parameter 
  9384.                 @profile_id = @real_profile_id,
  9385.                 @parameter_name = '-CommitBatchSize',
  9386.                 @parameter_value = '100'
  9387.         if (@retcode = 1 or @@ERROR <> 0)
  9388.             goto FAILURE
  9389.  
  9390.         exec @retcode = dbo.sp_add_agent_parameter 
  9391.                 @profile_id = @real_profile_id,
  9392.                 @parameter_name = '-CommitBatchThreshold',
  9393.                 @parameter_value = '1000'
  9394.         if (@retcode = 1 or @@ERROR <> 0)
  9395.             goto FAILURE
  9396.  
  9397.         exec @retcode = dbo.sp_add_agent_parameter 
  9398.                 @profile_id = @real_profile_id,
  9399.                 @parameter_name = '-HistoryVerboseLevel',
  9400.                 @parameter_value = '1'
  9401.         if (@retcode = 1 or @@ERROR <> 0)
  9402.             goto FAILURE
  9403.  
  9404.         exec @retcode = dbo.sp_add_agent_parameter 
  9405.                 @profile_id = @real_profile_id,
  9406.                 @parameter_name = '-LoginTimeout',
  9407.                 @parameter_value = '15'
  9408.         if (@retcode = 1 or @@ERROR <> 0)
  9409.             goto FAILURE
  9410.  
  9411.         exec @retcode = dbo.sp_add_agent_parameter 
  9412.                 @profile_id = @real_profile_id,
  9413.                 @parameter_name = '-MaxBcpThreads',
  9414.                 @parameter_value = '1'
  9415.         if (@retcode = 1 or @@ERROR <> 0)
  9416.             goto FAILURE
  9417.  
  9418.         exec @retcode = dbo.sp_add_agent_parameter 
  9419.                 @profile_id = @real_profile_id,
  9420.                 @parameter_name = '-MaxDeliveredTransactions',
  9421.                 @parameter_value = '0'
  9422.         if (@retcode = 1 or @@ERROR <> 0)
  9423.             goto FAILURE
  9424.  
  9425.         exec @retcode = dbo.sp_add_agent_parameter 
  9426.                 @profile_id = @real_profile_id,
  9427.                 @parameter_name = '-PollingInterval',
  9428.                 @parameter_value = '10'
  9429.         if (@retcode = 1 or @@ERROR <> 0)
  9430.             goto FAILURE
  9431.  
  9432.         exec @retcode = dbo.sp_add_agent_parameter 
  9433.                 @profile_id = @real_profile_id,
  9434.                 @parameter_name = '-QueryTimeout',
  9435.                 @parameter_value = '300'        -- 5 minutes 
  9436.         if (@retcode = 1 or @@ERROR <> 0)
  9437.             goto FAILURE
  9438.  
  9439.         exec @retcode = dbo.sp_add_agent_parameter 
  9440.                 @profile_id = @real_profile_id,
  9441.                 @parameter_name = '-TransactionsPerHistory',
  9442.                 @parameter_value = '100'
  9443.         if (@retcode = 1 or @@ERROR <> 0)
  9444.             goto FAILURE
  9445.  
  9446.         exec @retcode = dbo.sp_add_agent_parameter 
  9447.                 @profile_id = @real_profile_id,
  9448.                 @parameter_name = '-SkipErrors',
  9449.                 @parameter_value = '2601:2627:20598'
  9450.         if (@retcode = 1 or @@ERROR <> 0)
  9451.             goto FAILURE
  9452.  
  9453.         exec @retcode = dbo.sp_add_agent_parameter
  9454.                 @profile_id = @real_profile_id,
  9455.                 @parameter_name = '-KeepAliveMessageInterval',
  9456.                 @parameter_value = '300'
  9457.         if (@retcode = 1 or @@ERROR <> 0)
  9458.             goto FAILURE
  9459.  
  9460.         end
  9461.  
  9462.     /* 
  9463.     ** Merge agent : Rowcount Validation profile for high volumn server-to-server
  9464.     */
  9465.         else if (@profile_id = 15)
  9466.         begin
  9467.         exec @retcode = dbo.sp_add_agent_parameter 
  9468.                 @profile_id = @real_profile_id,
  9469.                 @parameter_name = '-PollingInterval',
  9470.                 @parameter_value = '60'
  9471.         if (@retcode = 1 or @@ERROR <> 0)
  9472.             goto FAILURE
  9473.  
  9474.         exec @retcode = dbo.sp_add_agent_parameter 
  9475.                 @profile_id = @real_profile_id,
  9476.                 @parameter_name = '-StartQueueTimeout',
  9477.                 @parameter_value = '0'
  9478.         if (@retcode = 1 or @@ERROR <> 0)
  9479.             goto FAILURE
  9480.  
  9481.         exec @retcode = dbo.sp_add_agent_parameter 
  9482.                 @profile_id = @real_profile_id,
  9483.                 @parameter_name = '-MaxDownloadChanges',
  9484.                 @parameter_value = '0'
  9485.         if (@retcode = 1 or @@ERROR <> 0)
  9486.             goto FAILURE
  9487.  
  9488.         exec @retcode = dbo.sp_add_agent_parameter 
  9489.                 @profile_id = @real_profile_id,
  9490.                 @parameter_name = '-MaxUploadChanges',
  9491.                 @parameter_value = '0'
  9492.         if (@retcode = 1 or @@ERROR <> 0)
  9493.             goto FAILURE
  9494.  
  9495.         exec @retcode = dbo.sp_add_agent_parameter 
  9496.                 @profile_id = @real_profile_id,
  9497.                 @parameter_name = '-ValidateInterval',
  9498.                 @parameter_value = '60'
  9499.         if (@retcode = 1 or @@ERROR <> 0)
  9500.             goto FAILURE
  9501.  
  9502.         exec @retcode = dbo.sp_add_agent_parameter 
  9503.                 @profile_id = @real_profile_id,
  9504.                 @parameter_name = '-LoginTimeout',
  9505.                 @parameter_value = '15'
  9506.         if (@retcode = 1 or @@ERROR <> 0)
  9507.             goto FAILURE
  9508.  
  9509.         exec @retcode = dbo.sp_add_agent_parameter 
  9510.                 @profile_id = @real_profile_id,
  9511.                 @parameter_name = '-QueryTimeout',
  9512.                 @parameter_value = '600'        
  9513.         if (@retcode = 1 or @@ERROR <> 0)
  9514.             goto FAILURE
  9515.  
  9516.         exec @retcode = dbo.sp_add_agent_parameter 
  9517.                 @profile_id = @real_profile_id,
  9518.                 @parameter_name = '-UploadGenerationsPerBatch',
  9519.                 @parameter_value = '2000'
  9520.         if (@retcode = 1 or @@ERROR <> 0)
  9521.             goto FAILURE
  9522.  
  9523.         exec @retcode = dbo.sp_add_agent_parameter 
  9524.                 @profile_id = @real_profile_id,
  9525.                 @parameter_name = '-DownloadGenerationsPerBatch',
  9526.                 @parameter_value = '2000'
  9527.         if (@retcode = 1 or @@ERROR <> 0)
  9528.             goto FAILURE
  9529.  
  9530.         exec @retcode = dbo.sp_add_agent_parameter 
  9531.                 @profile_id = @real_profile_id,
  9532.                 @parameter_name = '-UploadReadChangesPerBatch',
  9533.                 @parameter_value = '100'
  9534.         if (@retcode = 1 or @@ERROR <> 0)
  9535.             goto FAILURE
  9536.  
  9537.         exec @retcode = dbo.sp_add_agent_parameter 
  9538.                 @profile_id = @real_profile_id,
  9539.                 @parameter_name = '-DownloadReadChangesPerBatch',
  9540.                 @parameter_value = '100'
  9541.         if (@retcode = 1 or @@ERROR <> 0)
  9542.             goto FAILURE
  9543.  
  9544.         exec @retcode = dbo.sp_add_agent_parameter 
  9545.                 @profile_id = @real_profile_id,
  9546.                 @parameter_name = '-UploadWriteChangesPerBatch',
  9547.                 @parameter_value = '100'
  9548.         if (@retcode = 1 or @@ERROR <> 0)
  9549.             goto FAILURE
  9550.  
  9551.         exec @retcode = dbo.sp_add_agent_parameter 
  9552.                 @profile_id = @real_profile_id,
  9553.                 @parameter_name = '-DownloadWriteChangesPerBatch',
  9554.                 @parameter_value = '100'
  9555.         if (@retcode = 1 or @@ERROR <> 0)
  9556.             goto FAILURE
  9557.  
  9558.         exec @retcode = dbo.sp_add_agent_parameter 
  9559.                 @profile_id = @real_profile_id,
  9560.                 @parameter_name = '-Validate',
  9561.                 @parameter_value = '0'
  9562.         if (@retcode = 1 or @@ERROR <> 0)
  9563.             goto FAILURE
  9564.  
  9565.         exec @retcode = dbo.sp_add_agent_parameter 
  9566.                 @profile_id = @real_profile_id,
  9567.                 @parameter_name = '-FastRowCount',
  9568.                 @parameter_value = '1'
  9569.         if (@retcode = 1 or @@ERROR <> 0)
  9570.             goto FAILURE
  9571.         
  9572.         exec @retcode = dbo.sp_add_agent_parameter 
  9573.                 @profile_id = @real_profile_id,
  9574.                 @parameter_name = '-HistoryVerboseLevel',
  9575.                 @parameter_value = '1'
  9576.         if (@retcode = 1 or @@ERROR <> 0)
  9577.             goto FAILURE
  9578.  
  9579.         exec @retcode = dbo.sp_add_agent_parameter 
  9580.                 @profile_id = @real_profile_id,
  9581.                 @parameter_name = '-ChangesPerHistory',
  9582.                 @parameter_value = '100'
  9583.         if (@retcode = 1 or @@ERROR <> 0)
  9584.             goto FAILURE
  9585.  
  9586.         exec @retcode = dbo.sp_add_agent_parameter 
  9587.                 @profile_id = @real_profile_id,
  9588.                 @parameter_name = '-BcpBatchSize',
  9589.                 @parameter_value = '100000'
  9590.         if (@retcode = 1 or @@ERROR <> 0)
  9591.             goto FAILURE
  9592.  
  9593.         exec @retcode = dbo.sp_add_agent_parameter 
  9594.                 @profile_id = @real_profile_id,
  9595.                 @parameter_name = '-NumDeadlockRetries',
  9596.                 @parameter_value = '5'
  9597.         if (@retcode = 1 or @@ERROR <> 0)
  9598.             goto FAILURE
  9599.  
  9600.         exec @retcode = dbo.sp_add_agent_parameter 
  9601.                 @profile_id = @real_profile_id,
  9602.                 @parameter_name = '-SrcThreads',
  9603.                 @parameter_value = '3'
  9604.         if (@retcode = 1 or @@ERROR <> 0)
  9605.             goto FAILURE
  9606.  
  9607.         exec @retcode = dbo.sp_add_agent_parameter 
  9608.                 @profile_id = @real_profile_id,
  9609.                 @parameter_name = '-DestThreads',
  9610.                 @parameter_value = '4'
  9611.         if (@retcode = 1 or @@ERROR <> 0)
  9612.             goto FAILURE
  9613.  
  9614.         exec @retcode = dbo.sp_add_agent_parameter
  9615.                 @profile_id = @real_profile_id,
  9616.                 @parameter_name = '-KeepAliveMessageInterval',
  9617.                 @parameter_value = '300'
  9618.         if (@retcode = 1 or @@ERROR <> 0)
  9619.             goto FAILURE
  9620.  
  9621.         exec @retcode = dbo.sp_add_agent_parameter
  9622.                 @profile_id = @real_profile_id,
  9623.                 @parameter_name = '-MetadataRetentionCleanup',
  9624.                 @parameter_value = '1'
  9625.         if (@retcode = 1 or @@ERROR <> 0)
  9626.             goto FAILURE
  9627.  
  9628.         end
  9629.  
  9630.     return 0
  9631. FAILURE:
  9632.     return 1
  9633. go
  9634.  
  9635.  
  9636. raiserror('Creating procedure sp_MSvalidate_agent_parameter', 0,1)
  9637. go
  9638. create procedure sp_MSvalidate_agent_parameter (
  9639.     @profile_id      int,
  9640.     @parameter_name  sysname,
  9641.     @parameter_value nvarchar(255)
  9642. )
  9643. as
  9644.     declare @agent_type  int
  9645.     declare @original_parameter_name sysname
  9646.     declare @numeric_value int
  9647.  
  9648.     -- Make sure parameters are non-null
  9649.     if @profile_id is null
  9650.     BEGIN
  9651.         RAISERROR (14043, 16, -1, '@profile_id')
  9652.         RETURN (1)
  9653.     END
  9654.  
  9655.     if @parameter_name is null 
  9656.     BEGIN
  9657.         RAISERROR (14043, 16, -1, '@parameter_name')
  9658.         RETURN (1)
  9659.     END
  9660.  
  9661.     IF @parameter_value is null
  9662.     BEGIN
  9663.         RAISERROR (14043, 16, -1, '@parameter_value')
  9664.         RETURN (1)
  9665.     END
  9666.  
  9667.     select @original_parameter_name = @parameter_name
  9668.     
  9669.     select @agent_type = agent_type
  9670.     from msdb..MSagent_profiles 
  9671.     where profile_id = @profile_id
  9672.  
  9673.     -- Parameter name validation
  9674.     if (substring(@parameter_name, 1, 1) <> '/' and 
  9675.         substring(@parameter_name, 1, 1) <> '-')
  9676.     begin
  9677.         return 1
  9678.     end
  9679.  
  9680.     select @parameter_name = lower(substring(@parameter_name, 2, len(@parameter_name) - 1) collate SQL_Latin1_General_CP1_CS_AS)
  9681.  
  9682.     -- Snapshot agent - agent_type = 1
  9683.     if (@agent_type = 1)
  9684.     begin
  9685.         if not @parameter_name in ( 
  9686.             N'bcpbatchsize',   
  9687.             N'historyverboselevel',
  9688.             N'logintimeout',
  9689.             N'maxbcpthreads',
  9690.             N'querytimeout',
  9691.             N'startqueuetimeout',
  9692.             N'maxnetworkoptimization'
  9693.             )    
  9694.         begin
  9695.             raiserror(21111, 16, -1, @original_parameter_name)
  9696.             return 1
  9697.         end
  9698.     end
  9699.     -- Logreader - agent_type = 2
  9700.     else if (@agent_type =2)  
  9701.     begin
  9702.         if not lower(@parameter_name collate SQL_Latin1_General_CP1_CS_AS) in ( 
  9703.             N'historyverboselevel',
  9704.             N'logintimeout',
  9705.             N'pollinginterval',
  9706.             N'querytimeout',
  9707.             N'readbatchsize'
  9708.             )    
  9709.         begin
  9710.             raiserror(21112, 16, -1, @original_parameter_name)
  9711.             return 1
  9712.         end
  9713.     end
  9714.     -- Distribution agent - agent_type = 3
  9715.     else if (@agent_type = 3)
  9716.     begin
  9717.         if not @parameter_name in ( 
  9718.             N'bcpbatchsize',   
  9719.             N'commitbatchsize',   
  9720.             N'commitbatchthreshold',   
  9721.             N'historyverboselevel',
  9722.             N'logintimeout',
  9723.             N'maxbcpthreads',
  9724.             N'maxdeliveredtransactions',
  9725.             N'pollinginterval',
  9726.             N'querytimeout',
  9727.             N'transactionsperhistory',
  9728.             N'skiperrors',
  9729.             N'keepalivemessageinterval',
  9730.             N'useinprocloader'            
  9731.             )    
  9732.         begin
  9733.             raiserror(21113, 16, -1, @original_parameter_name)
  9734.             return 1
  9735.         end
  9736.     end
  9737.     -- Merge agent - agent_type = 4
  9738.     else if (@agent_type = 4)
  9739.     begin 
  9740.         if not @parameter_name in ( 
  9741.             N'startqueuetimeout',
  9742.             N'pollinginterval',   
  9743.             N'validateinterval',   
  9744.             N'logintimeout',   
  9745.             N'querytimeout',
  9746.             N'maxuploadchanges',
  9747.             N'maxdownloadchanges',
  9748.             N'uploadgenerationsperbatch',   
  9749.             N'downloadgenerationsperbatch',   
  9750.             N'uploadreadchangesperbatch',   
  9751.             N'downloadreadchangesperbatch',   
  9752.             N'uploadwritechangesperbatch',
  9753.             N'downloadwritechangesperbatch',
  9754.             N'validate',   
  9755.             N'fastrowcount',   
  9756.             N'historyverboselevel',
  9757.             N'changesperhistory',
  9758.             N'bcpbatchsize',
  9759.             N'numdeadlockretries',
  9760.             N'keepalivemessageinterval',
  9761.             N'srcthreads',
  9762.             N'destthreads',
  9763.             N'useinprocloader',
  9764.             N'metadataretentioncleanup'
  9765.             )    
  9766.         begin
  9767.             raiserror(21114, 16, -1, @original_parameter_name)
  9768.             return 1
  9769.         end
  9770.     end
  9771.     -- Qreader agent - agent_type = 9
  9772.     else if (@agent_type = 9)  
  9773.     begin
  9774.         if not lower(@parameter_name collate SQL_Latin1_General_CP1_CS_AS) in ( 
  9775.             N'resolverstate',
  9776.             N'sqlqueuemode',
  9777.             N'historyverboselevel',
  9778.             N'pollinginterval',
  9779.             N'logintimeout',
  9780.             N'querytimeout'
  9781.             )    
  9782.         begin
  9783.             raiserror(21112, 16, -1, @original_parameter_name)
  9784.             return 1
  9785.         end
  9786.     end
  9787.     else if @agent_type is null
  9788.     begin
  9789.         raiserror (20066, 16, -1)   -- profile not defined
  9790.         return 1
  9791.     end
  9792.     else
  9793.     begin
  9794.         -- MSagent_parameters table corruption
  9795.         return 1
  9796.     end
  9797.  
  9798.     -- Parameter value validation
  9799.     if (@parameter_name = N'bcpbatchsize')
  9800.     begin
  9801.         select @numeric_value = convert(int, @parameter_value)
  9802.         if @@error <> 0 or @numeric_value < 1
  9803.         begin
  9804.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9805.             return 1     
  9806.         end
  9807.     end
  9808.     else if (@parameter_name = N'commitbatchsize')
  9809.     begin
  9810.         select @numeric_value = convert(int, @parameter_value)
  9811.         if @@error <> 0 or @numeric_value < 1
  9812.         begin
  9813.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9814.             return 1     
  9815.         end                        
  9816.     end
  9817.     else if (@parameter_name = N'commitbatchthreshold')
  9818.     begin
  9819.         select @numeric_value = convert(int, @parameter_value)
  9820.         if @@error <> 0 or @numeric_value < 1
  9821.         begin
  9822.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9823.             return 1     
  9824.         end                        
  9825.     end
  9826.     else if (@parameter_name = N'downloadgenerationsperbatch')
  9827.     begin
  9828.         select @numeric_value = convert(int, @parameter_value)
  9829.         if @@error <> 0 or @numeric_value < 1
  9830.         begin
  9831.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9832.             return 1     
  9833.         end 
  9834.         if @numeric_value > 2000
  9835.         begin
  9836.             raiserror(14266, 16, -1, 'DownloadGenerationsPerBatch', '1 - 2000') 
  9837.             return 1
  9838.         end                      
  9839.     end
  9840.     else if (@parameter_name = N'downloadreadchangesperbatch')
  9841.     begin
  9842.         select @numeric_value = convert(int, @parameter_value)
  9843.         if @@error <> 0 or @numeric_value < 1
  9844.         begin
  9845.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9846.             return 1     
  9847.         end    
  9848.         if @numeric_value > 2000
  9849.         begin
  9850.             raiserror(14266, 16, -1, 'DownloadReadChangesPerBatch', '1 - 2000') 
  9851.             return 1
  9852.         end                      
  9853.     end
  9854.     else if (@parameter_name = N'downloadwritechangesperbatch')
  9855.     begin
  9856.         select @numeric_value = convert(int, @parameter_value)
  9857.         if @@error <> 0 or @numeric_value < 1
  9858.         begin
  9859.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9860.             return 1     
  9861.         end 
  9862.         if @numeric_value > 2000
  9863.         begin
  9864.             raiserror(14266, 16, -1, 'DownloadWriteChangesPerBatch', '1 - 2000') 
  9865.             return 1
  9866.         end                         
  9867.     end
  9868.     else if (@parameter_name = N'fastrowcount')
  9869.     begin
  9870.         select @numeric_value = convert(int, @parameter_value)
  9871.         if @@error <> 0 or not (@numeric_value in (1,2,3))
  9872.         begin
  9873.             raiserror(21116, 16, -1, @parameter_value, @original_parameter_name)
  9874.             return 1     
  9875.         end                        
  9876.     end
  9877.     else if (@parameter_name = N'historyverboselevel')
  9878.     begin
  9879.         select @numeric_value = convert(int, @parameter_value)
  9880.         if @@error <> 0 or not (@numeric_value in (0,1,2,3))
  9881.         begin
  9882.             raiserror(21117, 16, -1, @parameter_value, @original_parameter_name)
  9883.             return 1     
  9884.         end                        
  9885.     end
  9886.     else if (@parameter_name = N'logintimeout')
  9887.     begin
  9888.         select @numeric_value = convert(int, @parameter_value)
  9889.         if @@error <> 0 or @numeric_value < 1
  9890.         begin
  9891.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9892.             return 1     
  9893.         end                        
  9894.     end
  9895.     else if (@parameter_name = N'maxbcpthreads')
  9896.     begin
  9897.         select @numeric_value = convert(int, @parameter_value)
  9898.         if @@error <> 0 or @numeric_value < 1
  9899.         begin
  9900.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9901.             return 1     
  9902.         end                        
  9903.     end
  9904.     else if (@parameter_name = N'maxdeliveredtransactions')
  9905.     begin
  9906.         select @numeric_value = convert(int, @parameter_value)
  9907.         if @@error <> 0 or @numeric_value < 0
  9908.         begin
  9909.             raiserror(21119, 16, -1, @parameter_value, @original_parameter_name)
  9910.             return 1     
  9911.         end                        
  9912.     end
  9913.     else if (@parameter_name = N'pollinginterval')
  9914.     begin
  9915.         select @numeric_value = convert(int, @parameter_value)
  9916.         if @@error <> 0 or @numeric_value < 1
  9917.         begin
  9918.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9919.             return 1     
  9920.         end                        
  9921.     end
  9922.     else if (@parameter_name = N'querytimeout')
  9923.     begin
  9924.         select @numeric_value = convert(int, @parameter_value)
  9925.         if @@error <> 0 or @numeric_value < 1
  9926.         begin
  9927.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9928.             return 1     
  9929.         end                        
  9930.     end
  9931.     else if (@parameter_name = N'readbatchsize')
  9932.     begin
  9933.         select @numeric_value = convert(int, @parameter_value)
  9934.         if @@error <> 0 or @numeric_value < 1
  9935.         begin
  9936.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9937.             return 1     
  9938.         end                        
  9939.     end
  9940.     else if (@parameter_name = N'transactionsperhistory')
  9941.     begin
  9942.         select @numeric_value = convert(int, @parameter_value)
  9943.         if @@error <> 0 or @numeric_value not between 0 and 10000
  9944.         begin
  9945.             raiserror(211118, 16, -1, @parameter_value, @original_parameter_name)
  9946.             return 1     
  9947.         end                        
  9948.     end
  9949.     else if (@parameter_name = N'uploadgenerationsperbatch')
  9950.     begin
  9951.         select @numeric_value = convert(int, @parameter_value)
  9952.         if @@error <> 0 or @numeric_value < 1
  9953.         begin
  9954.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9955.             return 1     
  9956.         end 
  9957.         if @numeric_value > 2000
  9958.         begin
  9959.             raiserror(14266, 16, -1, 'UploadGenerationsPerBatch', '1 - 2000') 
  9960.             return 1
  9961.         end                         
  9962.     end
  9963.     else if (@parameter_name = N'uploadreadchangesperbatch')
  9964.     begin
  9965.         select @numeric_value = convert(int, @parameter_value)
  9966.         if @@error <> 0 or @numeric_value < 1
  9967.         begin
  9968.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9969.             return 1     
  9970.         end     
  9971.         if @numeric_value > 2000
  9972.         begin
  9973.             raiserror(14266, 16, -1, 'UploadReadChangesPerBatch', '1 - 2000') 
  9974.             return 1
  9975.         end                     
  9976.     end
  9977.     else if (@parameter_name = N'uploadwritechangesperbatch')
  9978.     begin
  9979.         select @numeric_value = convert(int, @parameter_value)
  9980.         if @@error <> 0 or @numeric_value < 1
  9981.         begin
  9982.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  9983.             return 1     
  9984.         end 
  9985.         if @numeric_value > 2000
  9986.         begin
  9987.             raiserror(14266, 16, -1, 'UploadWriteChangesPerBatch', '1 - 2000') 
  9988.             return 1
  9989.         end                         
  9990.     end
  9991.     else if (@parameter_name = N'validate')
  9992.     begin
  9993.         select @numeric_value = convert(int, @parameter_value)
  9994.         if @@error <> 0 or not (@numeric_value in (0,1,2,3))
  9995.         begin
  9996.             raiserror(21117, 16, -1, @parameter_value, @original_parameter_name)
  9997.             return 1     
  9998.         end                        
  9999.     end
  10000.     else if (@parameter_name = N'validateinterval')
  10001.     begin
  10002.         select @numeric_value = convert(int, @parameter_value)
  10003.         if @@error <> 0 or @numeric_value < 1
  10004.         begin
  10005.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10006.             return 1     
  10007.         end                        
  10008.     end
  10009.     else if (@parameter_name = N'skiperrors')
  10010.     begin
  10011.         -- Empty string is valid.
  10012.         if @parameter_value <> N''
  10013.         begin
  10014.             -- Valid format: 11:22:33
  10015.             if    @parameter_value like '%[^0-9:]%' or
  10016.                 @parameter_value like ':%' or
  10017.                 @parameter_value like '%:' or
  10018.                 @parameter_value like '%::%'
  10019.             begin
  10020.                 raiserror(20601, 16, -1)
  10021.                 return 1
  10022.             end
  10023.             -- cannot has number of errors equals to or more than 11
  10024.             if    @parameter_value like '%:%:%:%:%:%:%:%:%:%:%'
  10025.             begin
  10026.                 raiserror(20602, 16, -1)
  10027.                 return 1
  10028.             end
  10029.         end
  10030.     end
  10031.     -- Parameter value validation
  10032.     else if (@parameter_name = N'numdeadlockretries')
  10033.     begin
  10034.         select @numeric_value = convert(int, @parameter_value)
  10035.         if @@error <> 0 or @numeric_value < 1
  10036.         begin
  10037.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10038.             return 1     
  10039.         end
  10040.         if @numeric_value > 100
  10041.         begin
  10042.             raiserror(14266, 16, -1, 'NumDeadlockRetries', '1 - 100') 
  10043.             return 1
  10044.         end                         
  10045.     end
  10046.     else if (@parameter_name = N'srcthreads')
  10047.     begin
  10048.         select @numeric_value = convert(int, @parameter_value)
  10049.         if @@error <> 0 or @numeric_value < 1
  10050.         begin
  10051.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10052.             return 1     
  10053.         end                        
  10054.     end
  10055.     else if (@parameter_name = N'destthreads')
  10056.     begin
  10057.         select @numeric_value = convert(int, @parameter_value)
  10058.         if @@error <> 0 or @numeric_value < 1
  10059.         begin
  10060.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10061.             return 1     
  10062.         end                        
  10063.     end
  10064.     else if (@parameter_name = N'keepalivemessageinterval')
  10065.     begin
  10066.         select @numeric_value = convert(int, @parameter_value)
  10067.         if @@error <> 0 or @numeric_value < 30
  10068.         begin
  10069.             raiserror(21405, 16, -1, @parameter_value, @original_parameter_name, 30)            
  10070.             return 1
  10071.         end
  10072.     end
  10073.     else if (@parameter_name = N'useinprocloader')
  10074.     begin
  10075.         select @numeric_value = convert(int, @parameter_value)
  10076.         if @@error <> 0 or @numeric_value not in (0, 1) or rtrim(@parameter_value) = N''
  10077.         begin
  10078.             raiserror(21406, 16, -1, @parameter_value, @original_parameter_name)
  10079.             return 1
  10080.         end
  10081.     end
  10082.     else if (@parameter_name = N'startqueuetimeout')
  10083.     begin
  10084.         select @numeric_value = convert(int, @parameter_value)
  10085.         if @@error <> 0 or (@numeric_value < 300 and @numeric_value <> 0) or rtrim(@parameter_value) = N''
  10086.         begin
  10087.             raiserror(21404, 16, -1, @parameter_value, @original_parameter_name)
  10088.             return 1
  10089.         end
  10090.     end
  10091.     else if (@parameter_name = N'resolverstate')
  10092.     begin
  10093.         select @numeric_value = convert(int, @parameter_value)
  10094.         if @@error <> 0 or @numeric_value not in (1,2,3)
  10095.         begin
  10096.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10097.             return 1     
  10098.         end                        
  10099.     end
  10100.     else if (@parameter_name = N'sqlqueuemode')
  10101.     begin
  10102.         select @numeric_value = convert(int, @parameter_value)
  10103.         if @@error <> 0 or @numeric_value not in (0,1,2)
  10104.         begin
  10105.             raiserror(21115, 16, -1, @parameter_value, @original_parameter_name)
  10106.             return 1     
  10107.         end                        
  10108.     end
  10109.     else if (@parameter_name = N'maxnetworkoptimization')
  10110.     begin
  10111.         select @numeric_value = convert(int, @parameter_value)
  10112.         if @@error <> 0 or @numeric_value not in (0, 1) or rtrim(@parameter_value) = N''
  10113.         begin
  10114.             raiserror(21406, 16, -1, @parameter_value, @original_parameter_name)
  10115.             return 1
  10116.         end
  10117.     end
  10118.     
  10119.     return 0
  10120. go 
  10121.  
  10122. EXEC dbo.sp_MS_marksystemobject 'sp_MSvalidate_agent_parameter'
  10123.  
  10124.  
  10125. raiserror('Creating procedure sp_add_agent_parameter', 0,1)
  10126. go
  10127.  
  10128. -- Add a row into the "MSagent_parameters" table
  10129. create procedure sp_add_agent_parameter (
  10130.     @profile_id int,
  10131.     @parameter_name         sysname,
  10132.     @parameter_value        nvarchar(255)
  10133. )
  10134. as
  10135.     declare @slash_parameter_name sysname
  10136.     declare @dash_parameter_name sysname
  10137.     declare @retcode int
  10138.     
  10139.     set nocount on
  10140.  
  10141.     select @parameter_name = RTRIM(@parameter_name)
  10142.  
  10143.     -- Append leading '-' when not given
  10144.     if (substring(@parameter_name, 1, 1) <> '/' and 
  10145.         substring(@parameter_name, 1, 1) <> '-')
  10146.     BEGIN
  10147.         select @parameter_name = N'-' + @parameter_name
  10148.     END
  10149.  
  10150.     -- Call proc to validate parameter value
  10151.     exec @retcode = dbo.sp_MSvalidate_agent_parameter 
  10152.                         @profile_id = @profile_id, 
  10153.                         @parameter_name = @parameter_name,
  10154.                         @parameter_value = @parameter_value
  10155.     if @retcode <> 0
  10156.         RETURN(1)
  10157.  
  10158.  
  10159.     select @slash_parameter_name = lower(stuff(@parameter_name, 1, 1, N'/') collate SQL_Latin1_General_CP1_CS_AS)
  10160.     select @dash_parameter_name = lower(stuff(@parameter_name, 1, 1, N'-') collate SQL_Latin1_General_CP1_CS_AS)
  10161.      
  10162.  
  10163.     /* A parameter may be defined only once per profile */
  10164.     if exists ( select * from msdb..MSagent_parameters 
  10165.             where profile_id = @profile_id
  10166.             and (lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS) = @slash_parameter_name collate SQL_Latin1_General_CP1_CS_AS  
  10167.             or lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS) = @dash_parameter_name collate SQL_Latin1_General_CP1_CS_AS))
  10168.     BEGIN
  10169.         RAISERROR (20067, 16, -1, @parameter_name)  -- The parameter name ''%s'' already exists for the specified profile
  10170.         RETURN (1)
  10171.     END
  10172.  
  10173.     insert into msdb..MSagent_parameters
  10174.     values (@profile_id, @parameter_name, @parameter_value) ;
  10175.  
  10176.     if @@error <> 0
  10177.         return(1)
  10178. GO
  10179.  
  10180. raiserror('Creating procedure sp_change_agent_parameter', 0,1)
  10181. go
  10182. -- Change one parameter of a profile from the MSagent_parameters table
  10183. create procedure sp_change_agent_parameter(
  10184.     @profile_id int,
  10185.     @parameter_name         sysname,
  10186.     @parameter_value        nvarchar(255)
  10187. )AS
  10188.     declare @slash_parameter_name sysname
  10189.     declare @dash_parameter_name sysname
  10190.     declare @retcode int
  10191.  
  10192.     select @parameter_name = LTRIM(RTRIM(@parameter_name))
  10193.     select @parameter_value = LTRIM(RTRIM(@parameter_value))
  10194.  
  10195.     -- DMO sends in empty sting as '(null)'.
  10196.     if @parameter_value = N'(null)'
  10197.         select @parameter_value = N''
  10198.  
  10199.     -- Append leading '-' when not given
  10200.     if (substring(@parameter_name, 1, 1) <> '/' and 
  10201.         substring(@parameter_name, 1, 1) <> '-')
  10202.     BEGIN
  10203.         select @parameter_name = N'-' + @parameter_name
  10204.     END
  10205.  
  10206.     -- Call proc to validate parameter value
  10207.     exec @retcode = dbo.sp_MSvalidate_agent_parameter 
  10208.                         @profile_id = @profile_id, 
  10209.                         @parameter_name = @parameter_name,
  10210.                         @parameter_value = @parameter_value
  10211.     if @retcode <> 0
  10212.         RETURN(1)
  10213.  
  10214.  
  10215.     select @slash_parameter_name = lower(stuff(@parameter_name, 1, 1, N'/') collate SQL_Latin1_General_CP1_CS_AS)
  10216.     select @dash_parameter_name = lower(stuff(@parameter_name, 1, 1, N'-') collate SQL_Latin1_General_CP1_CS_AS)
  10217.    
  10218.     IF exists ( select * from msdb..MSagent_parameters 
  10219.             where profile_id = @profile_id and 
  10220.                 (lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS) = @slash_parameter_name collate SQL_Latin1_General_CP1_CS_AS or
  10221.                  lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS) = @dash_parameter_name collate SQL_Latin1_General_CP1_CS_AS))
  10222.     BEGIN
  10223.         update msdb..MSagent_parameters set value = @parameter_value where 
  10224.             profile_id = @profile_id and 
  10225.             (lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS) = @slash_parameter_name collate SQL_Latin1_General_CP1_CS_AS or
  10226.             lower(parameter_name collate SQL_Latin1_General_CP1_CS_AS) = @dash_parameter_name collate SQL_Latin1_General_CP1_CS_AS) 
  10227.  
  10228.         if @@error <> 0
  10229.             return(1)
  10230.     END
  10231. GO
  10232.  
  10233. raiserror('Creating procedure sp_change_agent_profile', 0,1)
  10234. go
  10235. -- Change one parameter of a profile from the MSagent_profiles table
  10236. create procedure sp_change_agent_profile(
  10237.     @profile_id         int,
  10238.     @property           sysname,
  10239.     @value              nvarchar(3000)
  10240. )AS
  10241.  
  10242.     IF lower(@property collate SQL_Latin1_General_CP1_CS_AS) not in ('description')
  10243.     BEGIN
  10244.         RAISERROR(21183, 16, -1, @property)
  10245.         RETURN (1)
  10246.     END
  10247.  
  10248.     IF exists ( select * from msdb..MSagent_profiles where profile_id = @profile_id)
  10249.     BEGIN
  10250.         update msdb..MSagent_profiles set description = @value where profile_id = @profile_id
  10251.         if @@error <> 0
  10252.             return(1)
  10253.     END
  10254. GO
  10255.  
  10256.  
  10257. raiserror('Creating procedure sp_help_agent_parameter', 0,1)
  10258. go
  10259. -- View all the parameters of a profile from the MSagent_parameters table
  10260. create procedure sp_help_agent_parameter(
  10261.     @profile_id     int = -1
  10262. )
  10263. as
  10264.     set nocount on
  10265.     
  10266.     if @profile_id = -1
  10267.     begin   
  10268.         select profile_id, parameter_name, value
  10269.         from msdb..MSagent_parameters
  10270.         order by profile_id, parameter_name
  10271.     end
  10272.     else
  10273.     begin
  10274.         select profile_id, parameter_name, value
  10275.         from msdb..MSagent_parameters
  10276.         where profile_id = @profile_id
  10277.         order by profile_id, parameter_name
  10278.     end
  10279. GO
  10280.  
  10281. raiserror('Creating procedure sp_MShelp_distdb', 0,1)
  10282. go
  10283.  
  10284. -- Called by the distribution agent to find the dist. db of a dist. publisher
  10285. create procedure sp_MShelp_distdb (     
  10286.     @publisher_name     sysname
  10287. )
  10288. as
  10289. if exists (select name from msdb..sysobjects where name = 'MSdistpublishers')
  10290. begin
  10291.     select distribution_db from msdb..MSdistpublishers
  10292.     where UPPER(name) = UPPER(@publisher_name) collate database_default
  10293. end
  10294. else 
  10295.     select NULL where 1=2   /* We still want an empty rowset for some agents */
  10296. GO
  10297.  
  10298. dump tran master with no_log
  10299. GO
  10300.  
  10301. raiserror('Creating procedure sp_MSupdate_replication_status', 0,1)
  10302. go
  10303. create procedure sp_MSupdate_replication_status
  10304. @publisher sysname,
  10305. @publisher_db sysname,
  10306. @publication sysname,
  10307. @publication_type int = 0,      -- 0 Transactional/Snapshot 1 Merge
  10308. @agent_type int,
  10309. @agent_name nvarchar(100),
  10310. @status int
  10311. as
  10312.  
  10313.     declare @deleted int
  10314.     declare @refresh int
  10315.     declare @getstatus int
  10316.     declare @dummy int
  10317.     declare @is_merge_agent bit
  10318.     
  10319.     if @agent_type = 4 or @agent_type = 0x80000004
  10320.         set @is_merge_agent = 1
  10321.     else
  10322.         set @is_merge_agent = 0
  10323.  
  10324.     set @deleted = -1
  10325.     set @refresh = -2            -- Status used to only update the timestamp column
  10326.     set @getstatus = -3            -- Get status of agent for dummy distribution row
  10327.  
  10328.  
  10329.     -- if table dne, then we're just installing distribution so we don't need to update status
  10330.     if (select object_id('tempdb.dbo.MSreplication_agent_status')) is NULL
  10331.         return 0
  10332.  
  10333.     -- If there are not rows in the table, we know that it is not loaded or used
  10334.     -- No need to refresh (better performance)
  10335.     -- At least, there will on row if loaded (see sp_MSload_replication_status)
  10336.     if not exists (select * from tempdb.dbo.MSreplication_agent_status)
  10337.         return 0
  10338.  
  10339.     if @status = @getstatus
  10340.     begin
  10341.         select @status = isnull(status, 0) from tempdb.dbo.MSreplication_agent_status where
  10342.             UPPER(publisher) = UPPER(@publisher) and
  10343.             publisher_db = @publisher_db and
  10344.             publication = 'ALL' and
  10345.             agent_type = @agent_type
  10346.     end            
  10347.  
  10348.     -- Update timestamp column via dummy update
  10349.     if @status = @refresh
  10350.     begin
  10351.         -- Dummy update to force timestamps to be updated.
  10352.         update tempdb.dbo.MSreplication_agent_status set status = status where
  10353.             UPPER(publisher) = UPPER(@publisher) and
  10354.             publisher_db = @publisher_db and
  10355.             publication like @publication -- Must use like as publication may be "%"
  10356.         return (0)
  10357.     end
  10358.  
  10359.     -- Remove row if @deleted
  10360.     if @status = @deleted
  10361.     begin
  10362.         if @@trancount > 0
  10363.         begin
  10364.             if exists (select * from tempdb.dbo.MSreplication_agent_status with (TABLOCKX) where 1 = 1)
  10365.             begin
  10366.                 select @dummy = 1
  10367.             end
  10368.         end        
  10369.  
  10370.         if @agent_name = '%' or @agent_name IS NULL
  10371.             delete from tempdb.dbo.MSreplication_agent_status with (TABLOCKX) where
  10372.                 UPPER(publisher) = UPPER(@publisher) and
  10373.                 publisher_db = @publisher_db and
  10374.                 publication = @publication and
  10375.                 agent_type = @agent_type 
  10376.         else
  10377.             delete from tempdb.dbo.MSreplication_agent_status with (TABLOCKX) where
  10378.                 UPPER(publisher) = UPPER(@publisher) and
  10379.                 publisher_db = @publisher_db and
  10380.                 publication = @publication and
  10381.                 agent_type = @agent_type and
  10382.                 agent_name = @agent_name
  10383.  
  10384.         -- Dummy update to force timestamps to be updated.  This will signal a row has been
  10385.         -- removed.
  10386.         update tempdb.dbo.MSreplication_agent_status set status = status where
  10387.             UPPER(publisher) = UPPER(@publisher) and
  10388.             publisher_db = @publisher_db and
  10389.             publication = @publication
  10390.         return (0)
  10391.     end     
  10392.  
  10393.     -- If misc. replication job then the status needs to be mapped.
  10394.     if @agent_type = 5
  10395.     begin
  10396.         set @status = 
  10397.         case isnull(@status,5)    -- mapped to never run
  10398.             when 0 then 5   -- Fail mapping
  10399.             when 1 then 2   -- Success mapping
  10400.             when 2 then 5   -- Retry mapping
  10401.             when 3 then 2   -- Shutdown mapping
  10402.             when 4 then 3   -- Inprogress mapping
  10403.             when 5 then 0   -- Unknown is mapped to never run
  10404.         end
  10405.     end
  10406.  
  10407.     if @is_merge_agent = 1
  10408.     begin
  10409.         update tempdb.dbo.MSreplication_agent_status set status = @status 
  10410.                 where agent_name = @agent_name and
  10411.                 publication = @publication and
  10412.                 UPPER(publisher) = UPPER(@publisher) and
  10413.                 publisher_db = @publisher_db and
  10414.                 agent_type = @agent_type
  10415.  
  10416.         if @@rowcount = 0
  10417.             insert into tempdb.dbo.MSreplication_agent_status 
  10418.                (publisher, publisher_db, publication, publication_type, agent_type, status, agent_name) values
  10419.                (@publisher, @publisher_db, @publication, @publication_type, @agent_type, @status, @agent_name)
  10420.     end
  10421.     else
  10422.     begin
  10423.         if not exists (select * from tempdb.dbo.MSreplication_agent_status where 
  10424.             UPPER(publisher) = UPPER(@publisher) and
  10425.             publisher_db = @publisher_db and
  10426.             publication = @publication and
  10427.             agent_type = @agent_type and
  10428.             agent_name = @agent_name)
  10429.             
  10430.             insert into tempdb.dbo.MSreplication_agent_status 
  10431.                     (publisher, publisher_db, publication, publication_type, agent_type, status, agent_name) values
  10432.                     (@publisher, @publisher_db, @publication, @publication_type, @agent_type, @status, @agent_name)
  10433.                 
  10434.         else
  10435.             update tempdb.dbo.MSreplication_agent_status set status = @status where
  10436.                 UPPER(publisher) = UPPER(@publisher) and
  10437.                 publisher_db = @publisher_db and
  10438.                 (publication = @publication or @publication = 'ALL')and
  10439.                 agent_type = @agent_type and
  10440.                 agent_name = @agent_name
  10441.     end                
  10442.  
  10443.     return (0)
  10444. go
  10445.  
  10446. raiserror('Creating procedure sp_MSenum_misc_agents', 0,1)
  10447. go
  10448. create procedure sp_MSenum_misc_agents
  10449. as
  10450.     SET NOCOUNT ON
  10451.  
  10452.     declare @timestamp timestamp
  10453.  
  10454.     -- Not supported, but returned to be consistent with other sp_MSenum_replication_agent
  10455.     -- result set.
  10456.     set @timestamp = 0
  10457.  
  10458.     select j.name, 'agent_type' = c.name,
  10459.             'status' = case isnull(jh.run_status, 5) -- mapped to never run
  10460.                 when 0 then 5   -- Fail mapping
  10461.                 when 1 then 2   -- Success mapping
  10462.                 when 2 then 5   -- Retry mapping
  10463.                 when 3 then 2   -- Shutdown mapping
  10464.                 when 4 then 3   -- Inprogress mapping
  10465.                 when 5 then 0   -- Unknown is mapped to never run
  10466.             end,
  10467.             jh.message, 'start_time' = convert(nvarchar(8), jh.run_date) + N' ' +
  10468.                                        stuff(stuff(right(convert(nvarchar(7), jh.run_time + 1000000), 6), 5, 0, N':'), 3, 0, N':') + 
  10469.                                        + N'.000',
  10470.             jh.run_duration, 
  10471.             'job_id' = convert(binary(16), j.job_id), 'local_timestamp' = @timestamp from 
  10472.                 msdb..sysjobs j
  10473.             LEFT OUTER JOIN msdb..sysjobhistory jh ON
  10474.             j.job_id = jh.job_id and 
  10475.             jh.instance_id = (select max(instance_id) from msdb..sysjobhistory jh2 where
  10476.                 jh2.job_id = j.job_id)
  10477.             INNER JOIN msdb..syscategories c ON
  10478.             j.category_id = c.category_id
  10479.             where j.category_id in (11, 12,16,17,18)
  10480.  
  10481.     order by j.job_id asc
  10482. go 
  10483.  
  10484. raiserror('Creating procedure sp_MSload_replication_status', 0,1)
  10485. go
  10486.  
  10487. create procedure sp_MSload_replication_status
  10488. as
  10489.     set nocount on
  10490.  
  10491.     declare @db_name sysname
  10492.     declare @distbit int
  10493.     declare @retcode int
  10494.  
  10495.     select @distbit = 16
  10496.     select @retcode = 0
  10497.  
  10498.     -- Flush out all current rows
  10499.     delete tempdb.dbo.MSreplication_agent_status
  10500.  
  10501.     -- Load misc. replication jobs status and map SQL Agent status to replication status
  10502.     -- If users delete all misc repl jobs, the status table may not be updated
  10503.     -- see sp_MSupdate_replication_status
  10504.     insert into tempdb.dbo.MSreplication_agent_status 
  10505.         select '', '', '', -1, 5, 
  10506.             case isnull(jh.run_status,5) -- mapped to never run
  10507.                 when 0 then 5   -- Fail mapping
  10508.                 when 1 then 2   -- Success mapping
  10509.                 when 2 then 5   -- Retry mapping
  10510.                 when 3 then 2   -- Shutdown mapping
  10511.                 when 4 then 3   -- Inprogress mapping
  10512.                 when 5 then 0   -- Unknown is mapped to never run
  10513.             end,
  10514.             j.name, NULL from msdb..sysjobs j
  10515.             LEFT OUTER JOIN msdb..sysjobhistory jh ON
  10516.             j.job_id = jh.job_id and 
  10517.             jh.instance_id = (select max(instance_id) from msdb..sysjobhistory jh2 where
  10518.                 jh2.job_id = j.job_id)
  10519.             where j.category_id in (11,12,16,17,18)
  10520.     if @@error <> 0
  10521.         return 1
  10522.  
  10523.     declare hCdatabase CURSOR LOCAL FAST_FORWARD FOR
  10524.         select name from master.dbo.sysdatabases 
  10525.             where
  10526.             category & @distbit <> 0 and
  10527.             has_dbaccess(name) = 1
  10528.         for read only
  10529.  
  10530.     open hCdatabase
  10531.     fetch next from hCdatabase into @db_name
  10532.     while (@@fetch_status <> -1)
  10533.     begin
  10534.  
  10535.         insert into tempdb.dbo.MSreplication_agent_status 
  10536.             (publisher, publisher_db, publication, publication_type, agent_type, status, agent_name)
  10537.             exec (@db_name + '.dbo.sp_MSenum_replication_status')
  10538.  
  10539.         fetch next from hCdatabase into @db_name
  10540.     end
  10541.         
  10542. DONE:
  10543.     close hCdatabase
  10544.     deallocate hCdatabase
  10545.  
  10546.  
  10547.     -- Add and initialize Perfmon SQL Replication Agents instances
  10548.     exec @retcode = dbo.sp_MSinit_replication_perfmon
  10549.     if @retcode <> 0 or @@error <> 0
  10550.         return 1
  10551.  
  10552. GO
  10553.  
  10554. raiserror('Creating procedure sp_MScreate_replication_status_table', 0,1)
  10555. go
  10556. create proc sp_MScreate_replication_status_table
  10557. as
  10558.     declare @retcode int
  10559.  
  10560.     if (select object_id('tempdb.dbo.MSreplication_agent_status')) is NULL
  10561.     begin
  10562.         -- begin tran
  10563.         create table tempdb.dbo.MSreplication_agent_status (
  10564.             publisher sysname NOT NULL,
  10565.             publisher_db sysname NOT NULL,
  10566.             publication sysname NOT NULL,
  10567.             publication_type int NOT NULL,          -- 0 transactional/snapshot  1 Merge
  10568.             agent_type int NOT NULL,
  10569.             status int NOT NULL,
  10570.             agent_name nvarchar(100) NOT NULL,
  10571.             timestamp NOT NULL
  10572.             )
  10573.         if @@error <> 0
  10574.             return 1
  10575.         
  10576.           create clustered index cMSreplication_agent_status ON tempdb.dbo.MSreplication_agent_status (agent_name)
  10577.         if @@error <> 0
  10578.             return 1
  10579.         create nonclustered index nc1MSreplication_agent_status ON 
  10580.             tempdb.dbo.MSreplication_agent_status (publication, publisher_db, publisher)
  10581.         if @@error <> 0
  10582.             return 1
  10583.         create nonclustered index nc2MSreplication_agent_status ON 
  10584.             tempdb.dbo.MSreplication_agent_status (agent_type)
  10585.         if @@error <> 0
  10586.             return 1
  10587.         create nonclustered index nc3MSreplication_agent_status ON 
  10588.             tempdb.dbo.MSreplication_agent_status (timestamp)
  10589.         if @@error <> 0
  10590.             return 1
  10591.     end
  10592.     return 0  -- If here, all is well and we're done.
  10593. go
  10594.  
  10595. raiserror('Creating procedure sp_MShelp_replication_status', 0,1)
  10596. go
  10597.  
  10598. create procedure sp_MShelp_replication_status (
  10599.     @publisher sysname = '%',
  10600.     @publisher_db sysname = '%',
  10601.     @publication sysname = '%',
  10602.     @agent_type int = 0,
  10603.     @exclude_anonymous bit = 0
  10604.     )
  10605. as
  10606. begin
  10607.     --set transaction isolation level read uncommitted
  10608.     
  10609.     declare @retcode int
  10610.             ,@max_status int
  10611.             ,@max_timestamp timestamp
  10612.             ,@publication_type int
  10613.             ,@all_status int
  10614.             ,@all_timestamp timestamp
  10615.             ,@distdbname sysname
  10616.             ,@anonymous_mask int
  10617.             ,@anonymous_bit_when_hide int
  10618.             ,@anonymous_bit_when_show int
  10619.             
  10620.     select @anonymous_mask = 0x80000000
  10621.     if @exclude_anonymous = 0
  10622.     begin
  10623.         select @anonymous_bit_when_hide = 0x00000000
  10624.                 ,@anonymous_bit_when_show = 0x80000000
  10625.     end
  10626.     else
  10627.     begin
  10628.         select @anonymous_bit_when_hide = 0x80000000
  10629.                 ,@anonymous_bit_when_show = 0x00000000
  10630.     end
  10631.  
  10632.     -- If the local distributor is not installed, we return nothing rather than
  10633.     -- fail with table not exists error.
  10634.     if (select object_id('tempdb.dbo.MSreplication_agent_status')) is NULL
  10635.         return 0
  10636.  
  10637.     -- If the temporary status table is empty populate it.
  10638.     -- If populated, there should be at lease history cleanup task.
  10639.     if not exists (select * from tempdb.dbo.MSreplication_agent_status)
  10640.     begin                                           
  10641.         exec @retcode = dbo.sp_MSload_replication_status
  10642.         if @@error <> 0
  10643.             return 1
  10644.     end
  10645.  
  10646.     
  10647.     -- @agent_type is only supported with all wildcards
  10648.     if @agent_type <> 0 and (@publisher <> '%' or @publisher_db <> '%' or @publication <> '%')
  10649.         return 1
  10650.     
  10651.     if @publisher = '%'
  10652.     begin
  10653.         -- @agent_type = 0 returns status of all agents including the misc. agents.
  10654.         -- @agent_type = -1 returns status of all agents except misc. agents.
  10655.         if @agent_type = 0 or @agent_type = -1
  10656.         begin
  10657.             select @max_status = max(status), @max_timestamp = max(timestamp) 
  10658.             from tempdb.dbo.MSreplication_agent_status (NOLOCK)
  10659.                 where ((@agent_type = -1 and agent_type <> 5) or
  10660.                       @agent_type = 0) and 
  10661.                       -- Filter out anonymous if needed
  10662.                       agent_type & @anonymous_bit_when_hide = 0
  10663.         end
  10664.         else
  10665.         begin
  10666.             select @max_status = max(status), @max_timestamp = max(timestamp) 
  10667.             from tempdb.dbo.MSreplication_agent_status (NOLOCK) 
  10668.             where
  10669.                 -- Turn off the anonymous bit when showing
  10670.                 agent_type & ~@anonymous_bit_when_show = @agent_type
  10671.         end
  10672.     end
  10673.     else if @publication = '%'
  10674.     begin
  10675.         select @max_status = max(status), @max_timestamp = max(timestamp)
  10676.         from tempdb.dbo.MSreplication_agent_status (NOLOCK) 
  10677.         where UPPER(publisher) = UPPER(@publisher) and 
  10678.               -- Filter out anonymous if needed
  10679.               agent_type & @anonymous_bit_when_hide = 0
  10680. --          publisher_db = @publisher_db 
  10681.  
  10682.         --
  10683.         -- Process the Queue reader entry separately and check if we need to pick them
  10684.         --
  10685.            select @distdbname = distribution_db 
  10686.                 from msdb.dbo.MSdistpublishers where name = @publisher collate database_default
  10687.         
  10688.         select @all_status = status, @all_timestamp = timestamp 
  10689.         from tempdb.dbo.MSreplication_agent_status (NOLOCK) 
  10690.         where agent_type = 9 and publisher = @@servername and publisher_db = @distdbname
  10691.  
  10692.         if @all_status > @max_status
  10693.             select @max_status = @all_status
  10694.  
  10695.         if @all_timestamp > @max_timestamp
  10696.             select @max_timestamp = @all_timestamp
  10697.     end
  10698.     else if @publication <> '%'
  10699.     begin
  10700.         select top 1 @max_status = status from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  10701.             UPPER(publisher) = UPPER(@publisher) and
  10702.             publisher_db = @publisher_db and
  10703.             publication = @publication  and 
  10704.             -- Filter out anonymous if needed
  10705.             agent_type & @anonymous_bit_when_hide = 0
  10706.             order by status desc
  10707.         
  10708.         -- Get publication type from the Snapshot agent of the Publication. This used to determine
  10709.         -- if the Logreader status should be included.
  10710.         select @publication_type = publication_type from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  10711.             UPPER(publisher) = UPPER(@publisher) and
  10712.             publisher_db = @publisher_db and
  10713.             publication = @publication and
  10714.             agent_type = 1    -- Snapshot agent
  10715.  
  10716.         select @max_timestamp = max(timestamp) from tempdb.dbo.MSreplication_agent_status (NOLOCK) where
  10717.             UPPER(publisher) = UPPER(@publisher) and
  10718.             publisher_db = @publisher_db and
  10719.             publication = @publication  and 
  10720.             -- Filter out anonymous if needed
  10721.             agent_type & @anonymous_bit_when_hide = 0
  10722.  
  10723.         -- If publication_type = 0 then transactional and must include the Logreader
  10724.         -- with publication = 'ALL'
  10725.         if @publication_type = 0
  10726.         begin
  10727.             select @all_status = status, @all_timestamp = timestamp 
  10728.             from tempdb.dbo.MSreplication_agent_status (NOLOCK) 
  10729.             where
  10730.                 UPPER(publisher) = UPPER(@publisher) and
  10731.                 publisher_db = @publisher_db and
  10732.                 publication = 'ALL' and
  10733.                 agent_type = 2 --Logreader
  10734.  
  10735.             if @all_status > @max_status
  10736.                 select @max_status = @all_status
  10737.  
  10738.             if @all_timestamp > @max_timestamp
  10739.                 select @max_timestamp = @all_timestamp
  10740.  
  10741.             --
  10742.             -- Process the Queue reader entry separately and check if we need to pick them
  10743.             --
  10744.                select @distdbname = distribution_db 
  10745.                     from msdb.dbo.MSdistpublishers where name = @publisher collate database_default
  10746.             
  10747.             declare @proc nvarchar(512)
  10748.                     ,@allow_queued_tran bit
  10749.                     
  10750.             SELECT @proc = quotename(@distdbname) + N'.dbo.sp_MSispublicationqueued'
  10751.             exec @retcode = @proc
  10752.                 @publisher = @publisher
  10753.                 ,@publisher_db = @publisher_db
  10754.                 ,@publication = @publication
  10755.                 ,@allow_queued_tran = @allow_queued_tran output
  10756.             if (@retcode != 0 and @@error != 0)
  10757.                 return 1
  10758.  
  10759.             if (@allow_queued_tran = 1)
  10760.             begin
  10761.                 select @all_status = status, @all_timestamp = timestamp
  10762.                 from tempdb.dbo.MSreplication_agent_status (NOLOCK) 
  10763.                 where agent_type = 9 and publisher = @@servername and publisher_db = @distdbname
  10764.  
  10765.                 if @all_status > @max_status
  10766.                     select @max_status = @all_status 
  10767.  
  10768.                 if @all_timestamp > @max_timestamp
  10769.                     select @max_timestamp = @all_timestamp
  10770.             end
  10771.         end
  10772.     end
  10773.  
  10774.     -- Return result set
  10775.     select 'status' = isnull(@max_status, 0), 'timestamp' = isnull (@max_timestamp, 0)
  10776.  
  10777.     --set transaction isolation level read committed
  10778.     return (0)
  10779. end
  10780. go
  10781.  
  10782. raiserror('Creating procedure sp_MSenum_replication_agents', 0,1)
  10783. go
  10784. create procedure sp_MSenum_replication_agents
  10785. @type int,
  10786. @exclude_anonymous bit = 0,
  10787. @check_user bit = 0
  10788. as
  10789.     set nocount on
  10790.     --set transaction isolation level read uncommitted
  10791.  
  10792.     declare @snapshot int
  10793.     declare @logreader int
  10794.     declare @distribution int
  10795.     declare @sproc sysname
  10796.     declare @db_name sysname
  10797.     declare @table_name sysname
  10798.     declare @cmd nvarchar(255)
  10799.     declare @distbit int
  10800.     declare @merge int
  10801.     declare @misc int
  10802.     declare @qreader int
  10803.  
  10804.     select @distbit = 16
  10805.  
  10806.     select @snapshot = 1
  10807.     select @logreader = 2
  10808.     select @distribution = 3
  10809.     select @merge = 4
  10810.     select @misc = 5
  10811.     select @qreader = 9
  10812.  
  10813.     if @type = @misc
  10814.     begin
  10815.         exec dbo.sp_MSenum_misc_agents
  10816.         return 0
  10817.     end
  10818.  
  10819.     if @type = @snapshot
  10820.     begin
  10821.         select @sproc = 'sp_MSenum_snapshot'
  10822.         create table #snapshot (dbname sysname collate database_default not null, name nvarchar(100) collate database_default not null, 
  10823.             status int NOT NULL,
  10824.             publisher sysname collate database_default not null, publisher_db sysname collate database_default not null, 
  10825.             publication sysname collate database_default not null,
  10826.             start_time nvarchar(24) collate database_default null, time nvarchar(24) collate database_default null, duration int NULL,
  10827.             comments nvarchar(255) collate database_default null, delivered_transactions int NULL, 
  10828.             delivered_commands int NULL, delivery_rate float NULL, error_id INT NULL,
  10829.             job_id binary(16) NULL, local_job bit NULL, profile_id int NOT NULL, 
  10830.             agent_id int NOT NULL, local_timestamp binary(8) NOT NULL)
  10831.  
  10832.         create unique clustered index ucsnapshot ON #snapshot (job_id)
  10833.  
  10834.         select @table_name = '#snapshot'
  10835.     end
  10836.     else if @type = @logreader
  10837.     begin
  10838.         select @sproc = 'sp_MSenum_logreader'
  10839.         create table #logreader (dbname sysname collate database_default not null, name nvarchar(100) collate database_default not null,  
  10840.             status int NOT NULL,
  10841.             publisher sysname collate database_default not null, publisher_db sysname collate database_default not null,
  10842.             start_time nvarchar(24) collate database_default null, time nvarchar(24) collate database_default null, duration int NULL,
  10843.             comments nvarchar(255) collate database_default null, delivery_time int NULL, 
  10844.             delivered_transactions int NULL, delivered_commands int NULL, 
  10845.             average_commands int NULL, delivery_rate int NULL, delivery_latency int NULL, 
  10846.             error_id INT NULL, job_id binary(16) NULL, local_job bit NULL,
  10847.             profile_id int NOT NULL, agent_id int NOT NULL, local_timestamp binary(8) NOT NULL)
  10848.  
  10849.         create unique clustered index uclogreader ON #logreader (job_id)
  10850.  
  10851.         select @table_name = '#logreader'
  10852.     end
  10853.     else if @type = @distribution
  10854.     begin
  10855.         select @sproc = 'sp_MSenum_distribution'
  10856.         create table #distribution (dbname sysname collate database_default not null, 
  10857.             name nvarchar(100) collate database_default not null,  
  10858.             status int NOT NULL,
  10859.             publisher sysname collate database_default not null, publisher_db sysname collate database_default not null, 
  10860.             publication sysname collate database_default null,
  10861.             subscriber sysname collate database_default null, subscriber_db sysname collate database_default null, subscription_type int NULL,
  10862.             start_time nvarchar(24) collate database_default null, time nvarchar(24) collate database_default null, duration int NULL,
  10863.             comments nvarchar(255) NULL, delivery_time int NULL, 
  10864.             delivered_transactions int NULL, delivered_commands int NULL, 
  10865.             average_commands int NULL, delivery_rate int NULL, 
  10866.             delivery_latency int NULL, error_id INT NULL,
  10867.             job_id binary(16) NULL, local_job bit NULL, profile_id int NOT NULL, 
  10868.             agent_id int NOT NULL, local_timestamp binary(8) NOT NULL, 
  10869.             offload_enabled bit NOT NULL, offload_server sysname collate database_default null,
  10870.             subscriber_type tinyint NULL)
  10871.  
  10872.         create unique clustered index ucdistribution ON #distribution (job_id)
  10873.  
  10874.         select @table_name = '#distribution'
  10875.     end
  10876.     else if @type = @merge
  10877.     begin
  10878.         select @sproc = 'sp_MSenum_merge'
  10879.         create table #merge (dbname sysname collate database_default not null, name nvarchar(100) collate database_default not null, 
  10880.             status int NOT NULL,
  10881.             publisher sysname collate database_default not null, publisher_db sysname collate database_default not null, publication sysname collate database_default null,
  10882.             subscriber sysname collate database_default null, subscriber_db sysname collate database_default null, subscription_type int NULL,
  10883.             start_time nvarchar(24) collate database_default null, time nvarchar(24) collate database_default null, duration int NULL, 
  10884.             comments nvarchar(255) NULL, delivery_rate int NULL,
  10885.             publisher_insertcount int NULL, publisher_updatecount int NULL, publisher_deletecount int NULL,
  10886.             publisher_conficts int NULL, 
  10887.             subscriber_insertcount int NULL, subscriber_updatecount int NULL, subscriber_deletecount int NULL,
  10888.             subscriber_conficts int NULL, error_id int NULL, job_id binary(16) NULL,
  10889.             local_job bit NULL, profile_id int NOT NULL, agent_id int NOT NULL, 
  10890.             local_timestamp binary(8) NOT NULL, offload_enabled bit NOT NULL, 
  10891.             offload_server sysname collate database_default null, subscriber_type tinyint NULL)
  10892.  
  10893.         create unique clustered index ucmerge ON #merge (job_id)
  10894.  
  10895.  
  10896.         select @table_name = '#merge'
  10897.     end
  10898.     else if @type = @qreader
  10899.     begin
  10900.         select @sproc = 'sp_MSenum_qreader'
  10901.         create table #qreader (dbname sysname collate database_default not null, 
  10902.             name nvarchar(100) collate database_default not null, status int NOT NULL,
  10903.             start_time nvarchar(24) collate database_default null, time nvarchar(24) collate database_default null, duration int NULL,
  10904.             comments nvarchar(255) collate database_default null, 
  10905.             transactions_processed int NULL, commands_processed int NULL, 
  10906.             average_commands int NULL, delivery_rate int NULL, delivery_latency int NULL,
  10907.             error_id INT NULL, job_id binary(16) NULL, 
  10908.             profile_id int NULL, agent_id int NOT NULL, local_timestamp binary(8) NOT NULL)
  10909.  
  10910.         create unique clustered index ucqreader ON #qreader (job_id)
  10911.  
  10912.         select @table_name = '#qreader'
  10913.     end
  10914.  
  10915.     declare hCdatabase CURSOR LOCAL FAST_FORWARD FOR
  10916.         select name from master.dbo.sysdatabases 
  10917.             where
  10918.             category & @distbit <> 0 and
  10919.             has_dbaccess(name) = 1
  10920.     for read only
  10921.  
  10922.     open hCdatabase
  10923.     fetch next from hCdatabase into @db_name
  10924.     while (@@fetch_status <> -1)
  10925.     begin
  10926.         
  10927.         -- Check to see if the user has permision to monitor the distribution 
  10928.         -- database.
  10929.         declare @has_pm bit
  10930.         select @cmd = quotename(@db_name) + '.dbo.sp_executesql'
  10931.         exec @cmd
  10932.             N'if is_member(N''db_owner'') = 1 or is_member(N''replmonitor'') = 1 set @has_pm = 1', 
  10933.             N'@has_pm bit output',
  10934.             @has_pm output
  10935.         if @has_pm = 1
  10936.         begin
  10937.             if @check_user = 1
  10938.             begin
  10939.                 select 'is_replmonitor' = 1
  10940.                 return
  10941.             end
  10942.             else
  10943.             begin
  10944.                 select @cmd = 'insert into ' + @table_name +
  10945.                     ' exec ' + quotename(@db_name) + '.dbo.' + @sproc + ' @show_distdb = 1' 
  10946.                     
  10947.                 if @type in (@distribution, @merge)
  10948.                     select @cmd = @cmd +
  10949.                         ', @exclude_anonymous = ' + cast(@exclude_anonymous as nvarchar(1))
  10950.  
  10951.                 exec (@cmd)
  10952.             end
  10953.         end
  10954.         fetch next from hCdatabase into @db_name
  10955.     end
  10956.     close hCdatabase
  10957.     deallocate hCdatabase
  10958.  
  10959.     -- Don't return agents if in check user mode
  10960.     if @check_user = 1
  10961.         return
  10962.  
  10963.  
  10964.     select @cmd = 'select * from ' + @table_name + ' order by job_id asc'
  10965.     exec (@cmd)
  10966.  
  10967.     -- Drop the table
  10968.     select @cmd = 'drop table ' + @table_name
  10969.     exec (@cmd)
  10970.     --set transaction isolation level read committed
  10971.  
  10972. GO
  10973.  
  10974. raiserror('Creating procedure sp_replication_agent_checkup', 0,1)
  10975. go
  10976. create procedure sp_replication_agent_checkup
  10977. @heartbeat_interval int = 10        -- minutes
  10978. as
  10979.     declare @distribution_db sysname
  10980.     declare @retstatus int
  10981.     declare @proc nvarchar(255)
  10982.     declare @retcode int
  10983.  
  10984.     declare hCdistdbs CURSOR LOCAL FAST_FORWARD for
  10985.         select name from msdb..MSdistributiondbs where 
  10986.             has_dbaccess(name) = 1
  10987.     for read only
  10988.     open hCdistdbs
  10989.     fetch hCdistdbs into @distribution_db
  10990.     while @@fetch_status <> -1
  10991.     begin
  10992.         select @proc = @distribution_db + '.dbo.sp_MSagent_stethoscope'
  10993.         execute  @retcode = @proc @heartbeat_interval
  10994.         if @@error <> 0 or @retcode <> 0
  10995.         begin
  10996.             select @retstatus = 1
  10997.             goto UNDO
  10998.         end
  10999.         fetch hCdistdbs into @distribution_db
  11000.     end
  11001.  
  11002.     set @retstatus = 0
  11003.  
  11004. UNDO:
  11005.     close hCdistdbs
  11006.     deallocate hCdistdbs
  11007.     return (@retstatus)
  11008. go
  11009.  
  11010. raiserror('Creating procedure sp_MScreate_replication_checkup_agent', 0,1)
  11011. go
  11012. create procedure sp_MScreate_replication_checkup_agent
  11013. @heartbeat_interval int = 10    -- minutes
  11014. as
  11015.     declare @command nvarchar(100)
  11016.     declare @retcode int
  11017.     declare @interval int
  11018.     declare @job_name nvarchar(100)
  11019.     declare @description nvarchar(100)
  11020.     declare @category_name sysname
  11021.  
  11022.     select @command = 'sp_replication_agent_checkup @heartbeat_interval = ' +
  11023.         convert(nvarchar(10), @heartbeat_interval)      
  11024.         
  11025.     -- Create job if it already exists
  11026.     select @job_name = formatmessage(20533)
  11027.  
  11028.     IF EXISTS (SELECT * FROM msdb..sysjobs_view WHERE name = @job_name collate database_default and
  11029.         UPPER(originating_server) = UPPER(CONVERT(sysname, SERVERPROPERTY('ServerName'))))
  11030.     BEGIN
  11031.         EXEC @retcode = msdb.dbo.sp_delete_job  @job_name = @job_name
  11032.         IF @@ERROR <> 0 or @retcode <> 0
  11033.         BEGIN
  11034.             return (1)            
  11035.         END
  11036.     END
  11037.  
  11038.     -- Create new job
  11039.     set @interval = convert(int, @heartbeat_interval)
  11040.     set @description = formatmessage(20534)
  11041.  
  11042.     -- Get Checkup category name (assumes category_id = 16)
  11043.     select @category_name = name FROM msdb.dbo.syscategories where category_id = 16
  11044.     EXECUTE @retcode = dbo.sp_MSadd_repl_job 
  11045.             @name = @job_name,
  11046.             @subsystem = 'TSQL', 
  11047.             @enabled = 1, 
  11048.             @command = @command,
  11049.             @description = @description,
  11050.             @freqtype = 4,
  11051.             @freqinterval = 1,
  11052.             @freqsubtype = 4,
  11053.             @freqsubinterval = @interval,
  11054.             @retryattempts = 0,
  11055.             @category_name = @category_name
  11056.     if @@ERROR <> 0 or @retcode <> 0
  11057.         return (1)
  11058. go
  11059.  
  11060. raiserror('Creating procedure sp_MSenum_replication_job', 0,1)
  11061. go
  11062. create procedure sp_MSenum_replication_job
  11063. @job_id uniqueidentifier
  11064. as
  11065.     SET NOCOUNT ON
  11066.     declare @message nvarchar(1024)
  11067.     declare @retcode int
  11068.     declare @runstatus int
  11069.     declare @date int
  11070.     declare @time int
  11071.  
  11072.     -- Get last message from SQL Agent History table
  11073.     create table #JobHistory (
  11074.         instance_id int NOT NULL, 
  11075.         job_id uniqueidentifier NOT NULL,
  11076.         job_name nvarchar(100) collate database_default not null,
  11077.         step_id int NOT NULL,
  11078.         step_name nvarchar(100) collate database_default not null, 
  11079.         sql_message_id int NOT NULL,
  11080.         sql_severity int NOT NULL,
  11081.         message nvarchar(1024) collate database_default null,
  11082.         run_status int NOT NULL,
  11083.         run_date int NOT NULL,
  11084.         run_time int NOT NULL,
  11085.         run_duration int NOT NULL,
  11086.         operator_emailed sysname collate database_default null,
  11087.         operator_netsent sysname collate database_default null,
  11088.         operator_paged sysname collate database_default null,
  11089.         retries_attempted int NOT NULL,
  11090.         server sysname collate database_default not null
  11091.     )
  11092.     if @@error <> 0
  11093.         return 1
  11094.  
  11095.     -- Insert last history for step_id 1 (Agent running)
  11096.     set rowcount 1
  11097.     insert into #JobHistory exec msdb.dbo.sp_help_jobhistory @job_id = @job_id, @step_id = 1, 
  11098.         @mode = 'FULL'          
  11099.  
  11100.     -- Get the last history
  11101.     select @message = message, @runstatus = run_status, @date = run_date, @time = run_time
  11102.         from #JobHistory
  11103.  
  11104.     -- Reset rowcount
  11105.     set rowcount 0
  11106.  
  11107.     -- Map SQL Agent runstatus to Replication runstatus
  11108.     set @runstatus = 
  11109.     case @runstatus
  11110.         when 0 then 6   -- Fail mapping
  11111.         when 1 then 2   -- Success mapping
  11112.         when 2 then 5   -- Retry mapping
  11113.         when 3 then 2   -- Shutdown mapping
  11114.         when 4 then 3   -- Inprogress mapping
  11115.         when 5 then 0   -- Unknown is mapped to never run
  11116.     end
  11117.  
  11118.     declare @sztime nchar(6)
  11119.     select @sztime = convert(nchar(6), @time)
  11120.  
  11121.     -- If the time is like 53030 (5:30:30), make it 053030
  11122.     if len(@sztime) = 5
  11123.         select @sztime = N'0' + @sztime
  11124.  
  11125.     -- Return status and message
  11126.     select 'runstatus' = isnull(@runstatus, 0), 'message' = @message,  'date' = @date, 
  11127.         'time' = @time, 'datetime' = 
  11128.             -- use this style 20000405 13:05:00.000, refer to sp_MSenumallsubscriptions
  11129.             convert(nchar(8), @date) + N' ' + 
  11130.             substring(@sztime, 1, 2) + N':' +
  11131.             substring(@sztime, 3, 2) + N':' +
  11132.             -- miliseconds are not availible
  11133.             substring(@sztime, 5, 2) + N'.000'
  11134.  
  11135.     drop table #JobHistory
  11136. go 
  11137.  
  11138. raiserror('Creating procedure sp_MSrepl_dbrole', 0,1)
  11139. go
  11140. CREATE PROCEDURE sp_MSrepl_dbrole
  11141.     @rolename       sysname,
  11142.     @loginname      sysname,
  11143.     @operation      nvarchar(10),
  11144.     @is_member      bit = 0 OUTPUT
  11145. AS
  11146.     -- This is an internal stored procedure. 
  11147.     -- If operation is 'add', it will add the login as a user to current database if necessary
  11148.     -- get the user name of the login and add it to the role
  11149.     -- If operation is 'drop', it will drop the user from the role.
  11150.     -- SETUP RUNTIME OPTIONS / DECLARE VARIABLES --
  11151.     declare @retcode int
  11152.     declare @username sysname
  11153.     declare @sid varbinary(85)
  11154.  
  11155.     select @sid = suser_sid(@loginname)
  11156.  
  11157.     if @operation = 'is_member'
  11158.     begin
  11159.         if exists (select * from sysusers r, sysusers u, sysmembers m where 
  11160.             u.sid = @sid and
  11161.             r.name = @rolename and
  11162.             m.groupuid = r.uid and
  11163.             m.memberuid = u.uid)
  11164.             select @is_member = 1
  11165.         else
  11166.             select @is_member = 0
  11167.         return(0)
  11168.     end
  11169.     
  11170.     -- Add the login to  db  role.
  11171.     if  is_srvrolemember('sysadmin',  @loginname) <> 1
  11172.     begin
  11173.         select @username = name from sysusers where sid = @sid
  11174.         if @operation = 'add'
  11175.         begin
  11176.             if @username is null
  11177.             begin
  11178.                 -- Add the server login to be a user with same name in the database
  11179.                 exec @retcode = dbo.sp_adduser  @loginname
  11180.                 if @retcode<>0 or @@error <> 0
  11181.                     return 1
  11182.                 select @username = @loginname
  11183.             end
  11184.  
  11185.             exec @retcode = dbo.sp_addrolemember @rolename, @username
  11186.             if @@error <> 0 OR @retcode <> 0
  11187.                 return 1
  11188.         end
  11189.         else if @operation = 'drop'
  11190.         begin
  11191.             if @username is not null
  11192.             begin
  11193.                 exec @retcode = dbo.sp_droprolemember @rolename, @username
  11194.                 if @@error <> 0 OR @retcode <> 0
  11195.                     return 1
  11196.             end
  11197.         end
  11198.     end     
  11199. go
  11200.  
  11201. raiserror('Creating procedure sp_changedistributor_password', 0,1)
  11202. go
  11203. CREATE PROCEDURE sp_changedistributor_password (
  11204.     @password sysname       
  11205.     ) AS
  11206.  
  11207.     declare @distributor sysname
  11208.     declare @distnetname sysname
  11209.     declare @retcode int
  11210.     declare @login sysname
  11211.  
  11212.     select @login = 'distributor_admin'
  11213.     SELECT @distributor = NULL
  11214.     SELECT @distributor = srvname, @distnetname = datasource 
  11215.         FROM master..sysservers
  11216.         WHERE  srvstatus & 8 <> 0
  11217.     
  11218.     exec @retcode = dbo.sp_addlinkedsrvlogin 
  11219.         @rmtsrvname= @distributor,
  11220.         @useself = 'false',
  11221.         @locallogin = NULL,
  11222.         @rmtuser = @login,
  11223.         @rmtpassword = @password
  11224.     IF @@error <> 0 OR @retcode <> 0
  11225.     BEGIN
  11226.         RETURN (1)
  11227.     END
  11228.  
  11229.     if UPPER(@distnetname) = UPPER(@@servername)
  11230.     begin
  11231.             -- Change the password if the distributor is local
  11232.         EXEC @retcode = dbo.sp_password NULL, @password, 'distributor_admin'
  11233.         if @@error <> 0 or @retcode <> 0
  11234.             return(1)
  11235.     end
  11236.  
  11237.     return (0)
  11238. GO
  11239.  
  11240. raiserror('Creating procedure sp_oledbinfo', 0,1)
  11241. go
  11242. CREATE PROCEDURE sp_oledbinfo
  11243.     @server nvarchar(128),
  11244.     @infotype nvarchar(128) = NULL,
  11245.     @login nvarchar(128) = NULL,
  11246.     @password nvarchar(128) = NULL
  11247.     AS
  11248.  
  11249.     SET NOCOUNT ON
  11250.  
  11251.     DECLARE @distributor sysname
  11252.     DECLARE @distproc nvarchar (255)
  11253.     DECLARE @retcode int
  11254.  
  11255.     /*
  11256.     ** Get distribution server information for remote RPC
  11257.     ** subscription calls.
  11258.     */
  11259.  
  11260.     EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT
  11261.     IF @@error <> 0 OR @retcode <> 0
  11262.         BEGIN
  11263.         RAISERROR (14071, 16, -1)
  11264.             RETURN (1)
  11265.     END
  11266.  
  11267.     select @distproc = rtrim(@distributor) + '.master.dbo.sp_MSget_oledbinfo'
  11268.     exec @retcode = @distproc @server, @infotype, @login, @password
  11269.     IF @@error <> 0
  11270.         BEGIN
  11271.         RAISERROR (14071, 16, -1)
  11272.         RETURN (1)
  11273.     END
  11274.  
  11275. go
  11276.  
  11277. raiserror('Creating procedure sp_MSget_oledbinfo', 0,1)
  11278. go
  11279. CREATE PROCEDURE sp_MSget_oledbinfo
  11280.     @server nvarchar(128),  -- the name by which the oledb datasource is referred to.
  11281.     @infotype nvarchar(128) = NULL,
  11282.     @login nvarchar(128) = NULL,
  11283.     @password nvarchar(128) = NULL
  11284.     AS
  11285.  
  11286.     SET NOCOUNT ON
  11287.  
  11288.     DECLARE @distproc nvarchar (255)
  11289.     DECLARE @providername nvarchar(256)
  11290.     DECLARE @datasource nvarchar(4000)
  11291.     DECLARE @location nvarchar(4000)
  11292.     DECLARE @providerstring nvarchar(4000)
  11293.     DECLARE @catalog nvarchar(256)
  11294.     DECLARE @retcode int
  11295.  
  11296.     select @providername = providername, @datasource = datasource, @location = location, @providerstring = providerstring, @catalog = catalog
  11297.         from master..sysservers where UPPER(srvname) = UPPER(@server) collate database_default
  11298.  
  11299.     if (@@rowcount = 0)
  11300.     begin
  11301.         raiserror(15015, 16, -1, @server)
  11302.         return (1)
  11303.     end
  11304.  
  11305.     exec @retcode = master..xp_oledbinfo @providername, @datasource, @location, @providerstring, @catalog, @login, @password, @infotype
  11306.     IF @@error <> 0
  11307.         BEGIN
  11308.         RAISERROR (14071, 16, -1)
  11309.         RETURN (1)
  11310.     END
  11311.  
  11312. go
  11313.  
  11314. raiserror('Creating procedure sp_grant_publication_access', 0,1)
  11315. go
  11316. CREATE PROCEDURE sp_grant_publication_access (
  11317.     @publication sysname,
  11318.     @login sysname,
  11319.     @reserved nvarchar(10) = NULL
  11320.         ) AS
  11321.  
  11322.     -- This stored procedure can be called repeatedly.
  11323.     DECLARE @distribdb sysname
  11324.     DECLARE @distproc nvarchar (300)
  11325.     DECLARE @retcode int
  11326.     DECLARE @dist_rpcname sysname
  11327.     DECLARE @database sysname
  11328.     
  11329.     -- Security Check
  11330.     exec @retcode = dbo.sp_MSreplcheck_publish
  11331.     if @@ERROR <> 0 or @retcode <> 0
  11332.         return(1)
  11333.     
  11334.     select @database = db_name()
  11335.  
  11336.     -- Existance check of the publication will be done in sp_MSpublication_access
  11337.     -- Note, even if the login exists, it may or may not has access
  11338.     -- to the server (granted or denied). 
  11339.     if @reserved is null
  11340.     begin
  11341.         if not exists (select * from master..syslogins where 
  11342.             sid = suser_sid(@login) and
  11343.             hasaccess = 1)
  11344.         begin
  11345.             raiserror(15007, 16, -1, @login) 
  11346.             return (1)
  11347.         end
  11348.         
  11349.         -- Don't do the user check if sysadmin since sysadmin can enter
  11350.         -- the database as dbo.
  11351.         if is_srvrolemember('sysadmin',@login) = 0 and
  11352.             not exists (select * from sysusers where
  11353.             (sid = suser_sid(@login) or name = N'guest') and
  11354.             hasdbaccess = 1) 
  11355.         begin
  11356.             raiserror(20619, 16, -1, @login, @database) 
  11357.             return (1)            
  11358.         end
  11359.     end
  11360.  
  11361.     select @database = db_name()
  11362.  
  11363.     EXEC @retcode = dbo.sp_helpdistributor  @rpcsrvname = @dist_rpcname OUTPUT,
  11364.                                         @distribdb   = @distribdb   OUTPUT
  11365.     IF @@error <> 0
  11366.     BEGIN
  11367.          RAISERROR (14071, 16, -1)
  11368.          return (1)
  11369.     END
  11370.  
  11371.     IF @retcode <> 0 OR @distribdb IS NULL OR @dist_rpcname IS NULL
  11372.     BEGIN
  11373.         RAISERROR (14071, 16, -1)
  11374.         return(1)
  11375.     END
  11376.  
  11377.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSpublication_access'
  11378.     
  11379.     declare @skip bit
  11380.  
  11381.     declare @login2 sysname
  11382.  
  11383.     if @reserved = 'init'
  11384.     begin
  11385.         -- Skip logins that are not at the distributor without
  11386.         -- raiseing error.
  11387.         set @skip = 1
  11388.         declare hC CURSOR LOCAL FAST_FORWARD for
  11389.             select loginname from master..syslogins where
  11390.                 (is_srvrolemember('sysadmin', loginname) = 1 or
  11391.                 sid = suser_sid()) 
  11392.         for read only
  11393.     end
  11394.     else
  11395.     begin
  11396.         set @skip = 0
  11397.         declare hC CURSOR LOCAL FAST_FORWARD for
  11398.                 select @login
  11399.             for read only
  11400.     end
  11401.  
  11402.     open hC
  11403.     fetch hC into @login2
  11404.  
  11405.     while (@@fetch_status <> -1)
  11406.     begin
  11407.         EXEC @retcode = @distproc
  11408.              @publisher = @@SERVERNAME,
  11409.              @publisher_db = @database,
  11410.              @publication = @publication,
  11411.              @login= @login2,
  11412.              @operation = 'add',
  11413.              @skip = @skip
  11414.         IF @@error <> 0 OR @retcode <> 0
  11415.             return (1)
  11416.  
  11417.         fetch hC into @login2
  11418.     end
  11419.     
  11420.     close hC
  11421.     deallocate hC
  11422. GO
  11423.  
  11424. raiserror('Creating procedure sp_revoke_publication_access', 0,1)
  11425. go
  11426. CREATE PROCEDURE sp_revoke_publication_access (
  11427.     @publication sysname,
  11428.     @login sysname
  11429.         ) AS
  11430.  
  11431.     -- This stored procedure can be called repeatedly.
  11432.     DECLARE @distribdb sysname
  11433.     DECLARE @distproc nvarchar (300)
  11434.     DECLARE @retcode int
  11435.     DECLARE @dist_rpcname sysname
  11436.     DECLARE @database sysname
  11437.  
  11438.     -- Security Check
  11439.     exec @retcode = dbo.sp_MSreplcheck_publish
  11440.     if @@ERROR <> 0 or @retcode <> 0
  11441.         return(1)
  11442.  
  11443.     -- Do check existense when dropping since the login might be dropped
  11444.     -- outside replication already.
  11445.  
  11446.     select @database = db_name()
  11447.  
  11448.     EXEC @retcode = dbo.sp_helpdistributor  @rpcsrvname = @dist_rpcname OUTPUT,
  11449.                                         @distribdb   = @distribdb   OUTPUT
  11450.     IF @@error <> 0
  11451.     BEGIN
  11452.          RAISERROR (14071, 16, -1)
  11453.          return (1)
  11454.     END
  11455.  
  11456.     IF @retcode <> 0 OR @distribdb IS NULL OR @dist_rpcname IS NULL
  11457.     BEGIN
  11458.         RAISERROR (14071, 16, -1)
  11459.         return(1)
  11460.     END
  11461.  
  11462.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSpublication_access'
  11463.     EXEC @retcode = @distproc
  11464.          @publisher = @@SERVERNAME,
  11465.          @publisher_db = @database,
  11466.          @publication = @publication,
  11467.          @login= @login,
  11468.          @operation = 'drop'
  11469.  
  11470.     IF @@error <> 0 OR @retcode <> 0
  11471.         return (1)
  11472. GO
  11473.  
  11474. raiserror('Creating procedure sp_help_publication_access', 0,1)
  11475. go
  11476. CREATE PROCEDURE sp_help_publication_access (
  11477.     @publication sysname,
  11478.     @return_granted bit = 1,
  11479.     @login sysname = '%',
  11480.     @initial_list bit = 0
  11481.         ) AS
  11482.  
  11483.     set nocount on
  11484.     -- This stored procedure can be called repeatedly.
  11485.     DECLARE @distribdb sysname
  11486.     DECLARE @distproc nvarchar (300)
  11487.     DECLARE @retcode int
  11488.     DECLARE @dist_rpcname sysname
  11489.     DECLARE @database sysname
  11490.  
  11491.     exec @retcode = dbo.sp_MSreplcheck_publish
  11492.     if @@ERROR <> 0 or @retcode <> 0
  11493.         return(1)
  11494.  
  11495.     -- Do check existense when dropping since the login might be dropped
  11496.     -- outside replication already.
  11497.  
  11498.     select @database = db_name()
  11499.  
  11500.     EXEC @retcode = dbo.sp_helpdistributor  @rpcsrvname = @dist_rpcname OUTPUT,
  11501.                                         @distribdb   = @distribdb   OUTPUT
  11502.     IF @@error <> 0
  11503.     BEGIN
  11504.          RAISERROR (14071, 16, -1)
  11505.          return (1)
  11506.     END
  11507.  
  11508.     IF @retcode <> 0 OR @distribdb IS NULL OR @dist_rpcname IS NULL
  11509.     BEGIN
  11510.         RAISERROR (14071, 16, -1)
  11511.         return(1)
  11512.     END
  11513.  
  11514.     SELECT @distproc = RTRIM(@dist_rpcname) + '.' + RTRIM(@distribdb) + '.dbo.sp_MSpublication_access'
  11515.  
  11516.     -- Get logins in the PAL if needed
  11517.     if @initial_list = 0
  11518.     begin
  11519.         create table #granted (login sysname collate database_default null)    
  11520.         insert into #granted EXEC @retcode = @distproc
  11521.              @publisher = @@SERVERNAME,
  11522.              @publisher_db = @database,
  11523.              @publication = @publication,
  11524.              @operation = 'help',
  11525.              @login = @login
  11526.  
  11527.         IF @@error <> 0 OR @retcode <> 0
  11528.             return (1)
  11529.     end
  11530.  
  11531.     -- Get distributor valid logins if needed
  11532.     if @return_granted = 0 or @initial_list = 1
  11533.     begin
  11534.         create table #dist_logins(login sysname collate database_default null)
  11535.         insert into #dist_logins EXEC @retcode = @distproc
  11536.              @publisher = @@SERVERNAME,
  11537.              @operation = 'get_logins'
  11538.     end
  11539.  
  11540.     if @initial_list = 1
  11541.     begin
  11542.         -- Get the initial list for the publication to be created
  11543.         -- by the current user
  11544.         -- It contains all the logins sysadmin group and the current user
  11545.         -- that have valid login at the distributor.
  11546.         select l.loginname, l.isntname, l.isntgroup
  11547.          from master..syslogins l, #dist_logins d where
  11548.             l.sid = suser_sid(d.login) and
  11549.             l.hasaccess = 1 and
  11550.             (is_srvrolemember('sysadmin',d.login) = 1 or (l.sid = suser_sid())) 
  11551.     end
  11552.     else if @return_granted = 0
  11553.     begin
  11554.         select l.loginname, l.isntname, l.isntgroup
  11555.          from master..syslogins l, #dist_logins d where
  11556.             l.sid = suser_sid(d.login) and
  11557.             l.hasaccess = 1 and 
  11558.             not exists (select * from #granted g where 
  11559.                 suser_sid(g.login) = l.sid) and
  11560.             exists (select * from sysusers u where
  11561.                 (u.sid = l.sid or u.name = 'guest') and
  11562.                 u.hasdbaccess = 1)
  11563.     end
  11564.     else if @return_granted = 1
  11565.     begin
  11566.         select l.loginname, l.isntname, l.isntgroup from master..syslogins l,
  11567.             #granted g where
  11568.             l.sid = suser_sid(g.login)
  11569.     end
  11570. GO
  11571.  
  11572. raiserror('Creating procedure sp_check_publication_access', 0,1)
  11573. go
  11574. CREATE PROCEDURE sp_check_publication_access
  11575. @publication sysname,
  11576. @given_login sysname = NULL
  11577. AS
  11578.     set nocount on
  11579.     declare @retcode int
  11580.     exec @retcode = dbo.sp_MSreplcheck_pull 
  11581.         @publication = @publication,
  11582.         @given_login = @given_login
  11583.     if @retcode <> 0 or @@error <> 0
  11584.         return (1)
  11585.     return 0
  11586. GO
  11587.  
  11588. exec dbo.sp_MS_marksystemobject sp_check_publication_access
  11589. go
  11590.  
  11591. raiserror('Creating procedure sp_MSget_agent_names', 0,1)
  11592. go
  11593. CREATE PROCEDURE sp_MSget_agent_names
  11594. @publication sysname,        
  11595. @subscriber sysname = NULL,              
  11596. @subscriber_db sysname = NULL,
  11597. @publisher sysname = NULL,
  11598. @publisher_db sysname = NULL
  11599. as
  11600.     set nocount on
  11601.  
  11602.     declare @retcode int
  11603.  
  11604.     if @publisher is null
  11605.     -- We are at the publisher side, call into the distributor
  11606.     begin
  11607.         /*
  11608.         ** Get distribution server information for remote RPC
  11609.         ** agent verification.
  11610.         */
  11611.         declare @distributor sysname
  11612.         declare @distribdb sysname
  11613.         declare @distproc nvarchar(1000), @pubdb sysname
  11614.  
  11615.         select @pubdb = db_name()
  11616.  
  11617.         EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  11618.             @distribdb = @distribdb OUTPUT
  11619.  
  11620.         IF @@error <> 0 OR @retcode <> 0
  11621.         BEGIN
  11622.             RAISERROR (14071, 16, -1)
  11623.             RETURN (1)
  11624.         END
  11625.  
  11626.         /*
  11627.         ** Call proc to change the distributor
  11628.         */
  11629.         SELECT @distproc = RTRIM(@distributor) + '.' + @distribdb + 
  11630.             '.dbo.sp_MSget_agent_names'
  11631.         exec @retcode = @distproc 
  11632.             @publisher = @@SERVERNAME,
  11633.             @publisher_db = @pubdb,
  11634.             @publication = @publication,
  11635.             @subscriber = @subscriber,
  11636.             @subscriber_db = @subscriber_db
  11637.     end
  11638.     else
  11639.     -- We are at the distribution db
  11640.     begin
  11641.         declare @publisher_id smallint
  11642.         select @publisher_id = srvid from master..sysservers where UPPER(srvname) = UPPER(@publisher) collate database_default
  11643.  
  11644.         if @subscriber is null
  11645.         -- Returning publication agents if @subscriber is null
  11646.         begin
  11647.             declare @snapshot_agent sysname, @logreader_agent sysname, @qreader_agent sysname
  11648.             
  11649.             -- Have to use name in sysjobs table because users may rename
  11650.             -- the SQLServerAgent jobs. If users deleted the jobs, DMO scripting will
  11651.             -- create new jobs.
  11652.  
  11653.             select 
  11654.                 'snapshot_agent' = (select j.name from MSsnapshot_agents a, msdb..sysjobs j where
  11655.                     publisher_id = @publisher_id and
  11656.                     publisher_db = @publisher_db collate database_default and
  11657.                     publication = @publication collate database_default and
  11658.                     a.job_id = j.job_id),
  11659.                 'logreader_agent' = (select j.name from MSlogreader_agents a, msdb..sysjobs j where
  11660.                     publisher_id = @publisher_id and
  11661.                     publisher_db = @publisher_db collate database_default and
  11662.                     a.job_id = j.job_id),
  11663.                 'qreader_agent' = (select j.name from MSqreader_agents a, msdb..sysjobs j where
  11664.                     a.job_id = j.job_id)
  11665.         end
  11666.         else
  11667.         begin
  11668.             declare @subscriber_id smallint
  11669.             select @subscriber_id = srvid from master..sysservers where UPPER(srvname) = UPPER(@subscriber) collate database_default
  11670.             declare @publication_type int, @independent_agent bit
  11671.  
  11672.             select @publication_type = publication_type, @independent_agent = independent_agent
  11673.                 from MSpublications where 
  11674.                     publisher_id = @publisher_id and
  11675.                     publisher_db = @publisher_db and
  11676.                     publication = @publication
  11677.  
  11678.             if @publication_type = 2
  11679.             -- Merge publication, get merge agent name
  11680.             begin
  11681.                 select 'merge_agent' = j.name from MSmerge_agents a, msdb..sysjobs j where
  11682.                     publisher_id = @publisher_id and
  11683.                     publisher_db = @publisher_db collate database_default and
  11684.                     publication = @publication collate database_default and
  11685.                     subscriber_id = @subscriber_id and
  11686.                     subscriber_db = @subscriber_db collate database_default and
  11687.                     a.job_id = j.job_id
  11688.             end
  11689.             else
  11690.             begin
  11691.                 select 'distribution_agent' = j.name from MSdistribution_agents a, msdb..sysjobs j where
  11692.                     publisher_id = @publisher_id and
  11693.                     publisher_db = @publisher_db collate database_default and
  11694.                     (publication = @publication collate database_default or
  11695.                      publication = N'ALL' and @independent_agent = 0) and
  11696.                     subscriber_id = @subscriber_id and
  11697.                     subscriber_db = @subscriber_db collate database_default and
  11698.                     a.job_id = j.job_id
  11699.             end
  11700.         end
  11701.     end -- If @publisher is null
  11702.  
  11703. go
  11704.  
  11705. exec dbo.sp_MS_marksystemobject sp_MSget_agent_names
  11706. go
  11707.  
  11708. raiserror('Creating procedure sp_MSinit_replication_perfmon', 0,1)
  11709. go
  11710.  
  11711. create proc sp_MSinit_replication_perfmon
  11712. as
  11713.     declare @agent_type int
  11714.     declare @agent_name nvarchar(100)
  11715.     declare @status int
  11716.  
  11717.     -- Remove all existing instances
  11718.     dbcc deleteinstance ("SQL Replication Agents", "%")
  11719.     dbcc deleteinstance ("SQL Replication Snapshot", "%")
  11720.     dbcc deleteinstance ("SQL Replication Logreader", "%")
  11721.     dbcc deleteinstance ("SQL Replication Distribution", "%")
  11722.     dbcc deleteinstance ("SQL Replication Merge", "%")
  11723.     dbcc deleteinstance ("SQL Replication Queuereader", "%")
  11724.  
  11725.     -- Add and initialize Perfmon SQL Replication Agents instances
  11726.     dbcc addinstance ("SQL Replication Agents", "Snapshot")
  11727.     dbcc addinstance ("SQL Replication Agents", "Logreader")
  11728.     dbcc addinstance ("SQL Replication Agents", "Distribution")
  11729.     dbcc addinstance ("SQL Replication Agents", "Merge")
  11730.     dbcc addinstance ("SQL Replication Agents", "Queuereader")
  11731.     dbcc setinstance ("SQL Replication Agents", "Running", "Snapshot", 0)
  11732.     dbcc setinstance ("SQL Replication Agents", "Running", "Logreader", 0)
  11733.     dbcc setinstance ("SQL Replication Agents", "Running", "Distribution", 0)
  11734.     dbcc setinstance ("SQL Replication Agents", "Running", "Merge", 0)
  11735.     dbcc setinstance ("SQL Replication Agents", "Running", "Queuereader", 0)
  11736.  
  11737.     -- Add instances for each agent currently in the status table
  11738.     declare hC CURSOR LOCAL FAST_FORWARD for select agent_type, agent_name, status from tempdb.dbo.MSreplication_agent_status for read only
  11739.     open hC
  11740.     fetch hC into @agent_type, @agent_name, @status
  11741.     while (@@fetch_status <> -1)
  11742.     begin
  11743.  
  11744.         if @agent_type = 1 
  11745.         begin
  11746.             dbcc addinstance ("SQL Replication Snapshot", @agent_name)
  11747.             if @status = 1 or @status = 3 or @status = 4
  11748.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Snapshot", 1)
  11749.         end
  11750.         else if @agent_type = 2 
  11751.         begin
  11752.             dbcc addinstance ("SQL Replication Logreader", @agent_name)
  11753.             if @status = 1 or @status = 3 or @status = 4
  11754.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Logreader", 1)
  11755.         end
  11756.         else if @agent_type = 3 
  11757.         begin
  11758.             dbcc addinstance ("SQL Replication Distribution", @agent_name)
  11759.             if @status = 1 or @status = 3 or @status = 4
  11760.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Distribution", 1)
  11761.         end
  11762.         else if @agent_type = 4 
  11763.         begin
  11764.             dbcc addinstance ("SQL Replication Merge", @agent_name)
  11765.             if @status = 1 or @status = 3 or @status = 4
  11766.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Merge", 1)
  11767.         end
  11768.         else if @agent_type = 9 
  11769.         begin
  11770.             dbcc addinstance ("SQL Replication Queuereader", @agent_name)
  11771.             if @status = 1 or @status = 3 or @status = 4
  11772.                 dbcc incrementinstance ("SQL Replication Agents", "Running", "Queuereader", 1)
  11773.         end
  11774.  
  11775.         fetch hC into @agent_type, @agent_name, @status
  11776.     end
  11777.     close hC
  11778.     deallocate hC
  11779. GO
  11780.  
  11781. raiserror('Creating procedure sp_MSrepl_startup', 0,1)
  11782. go
  11783. create procedure sp_MSrepl_startup
  11784. as
  11785.  
  11786.     -- Drop and create publisher side cache table
  11787.     if exists (select * from tempdb..sysobjects where name = 'MSpublisher_access' and
  11788.     type = 'U')
  11789.         drop table tempdb.dbo.MSpublisher_access
  11790.  
  11791.     create table tempdb.dbo.MSpublisher_access
  11792.     (
  11793.     spid int NOT NULL,
  11794.     db_id int not null,
  11795.     publication sysname not null,
  11796.     login_time datetime not null,
  11797.     pubid uniqueidentifier null -- Used for merge only.
  11798.     )
  11799.  
  11800.     CREATE CLUSTERED INDEX ucMSpublisher_access ON tempdb.dbo.MSpublisher_access
  11801.         (spid, publication, db_id)
  11802.  
  11803.     -- Drop and create distributor side cache table
  11804.     -- We need to avoid publisher and distributor using the same table to prevent
  11805.     -- contention in local distributor case.
  11806.     if exists (select * from master..sysservers
  11807.                   WHERE  srvstatus & 8 <> 0 and UPPER(datasource) = UPPER(@@servername) collate database_default)
  11808.     begin
  11809.         if exists (select * from tempdb..sysobjects where name = 'MSdistributor_access' and
  11810.         type = 'U')
  11811.             drop table tempdb.dbo.MSdistributor_access
  11812.  
  11813.         create table tempdb.dbo.MSdistributor_access
  11814.         (
  11815.         spid int NOT NULL,
  11816.         db_id int not null,
  11817.         agent_id int not null,
  11818.         agent_type int not null,
  11819.         publication_id int not null,
  11820.         login_time datetime not null
  11821.         )
  11822.  
  11823.         CREATE CLUSTERED INDEX ucMSdistributor_access ON tempdb.dbo.MSdistributor_access
  11824.             (spid, agent_id, db_id)
  11825.  
  11826.         -- Create repl monitor table
  11827.         exec sp_MScreate_replication_status_table
  11828.    end
  11829. go
  11830.  
  11831. -- If a distributor is installed, mark the sp as a startup sp. 
  11832. if exists (select * FROM master..sysservers WHERE  srvstatus & 8 <> 0)
  11833.     exec dbo.sp_procoption 'sp_MSrepl_startup', 'startup', 'true' 
  11834. go
  11835.  
  11836. raiserror('Creating procedure sp_MSflush_access_cache', 0,1)
  11837. go
  11838. CREATE PROCEDURE sp_MSflush_access_cache 
  11839. AS
  11840.     -- Delete all the 'dead' connections in MSpublisher_access.
  11841.     delete tempdb.dbo.MSpublisher_access 
  11842.         from tempdb.dbo.MSpublisher_access a
  11843.         where not exists (select * from master..sysprocesses p where
  11844.             a.spid = p.spid and
  11845.             p.login_time = a.login_time)
  11846.     if @@error <> 0
  11847.         return 1
  11848.     else
  11849.         return 0
  11850.  
  11851.     if exists (select * from tempdb..sysobjects where name = 'MSdistributor_access' and
  11852.     type = 'U')
  11853.     begin
  11854.         -- Delete all the 'dead' connections in MSdistributor_access.
  11855.         delete tempdb.dbo.MSdistributor_access 
  11856.             from tempdb.dbo.MSdistributor_access a
  11857.             where not exists (select * from master..sysprocesses p where
  11858.                 a.spid = p.spid and
  11859.                 p.login_time = a.login_time)
  11860.         if @@error <> 0
  11861.             return 1
  11862.         else
  11863.             return 0
  11864.     end
  11865.     return (0)
  11866. GO
  11867.  
  11868. raiserror('Creating procedure sp_MSreinit_failed_subscriptions', 0,1)
  11869. go
  11870. -- This stored procedure is used as a response to the Replication Validation Failure Alert.
  11871. -- It will reinit the failed subscription. If the publisher is remote, it must be configured as a remote server 
  11872. -- for this procedure to work.
  11873. create proc sp_MSreinit_failed_subscriptions
  11874. @failure_level int = 0      -- 0 All failure  1 Validation failures
  11875. as
  11876.  
  11877.     declare @publisher sysname
  11878.     declare @publisher_db sysname
  11879.     declare @publication sysname
  11880.     declare @article sysname
  11881.     declare @publication_type int
  11882.     declare @subscriber sysname
  11883.     declare @subscriber_db sysname
  11884.     declare @agent_type int
  11885.     declare @alert_id int
  11886.     declare @proc nvarchar(100)
  11887.     declare @message nvarchar(4000)
  11888.     declare @retcode int
  11889.     declare @found bit
  11890.     declare @return_value int
  11891.     
  11892.     set nocount on
  11893.  
  11894.     set @found = 0          -- set if cursor returns a row
  11895.     set @return_value = 0   -- set to success
  11896.  
  11897.     -- For each publication validation failure, resync the subscription
  11898.     declare hc CURSOR LOCAL FAST_FORWARD for select publisher, publisher_db, publication, publication_type, article, subscriber, 
  11899.         subscriber_db, alert_id from 
  11900.         msdb..sysreplicationalerts where
  11901.         (@failure_level = 0 or (@failure_level = 1 and alert_error_code = 20574)) and   -- 20574 = validation failure
  11902.         status = 0
  11903.         for read only
  11904.  
  11905.     open hc
  11906.     fetch hc into  @publisher, @publisher_db, @publication, @publication_type, @article, @subscriber, @subscriber_db, @alert_id
  11907.     while (@@fetch_status <> -1)
  11908.     begin
  11909.         
  11910.         set @found = 1
  11911.  
  11912.         -- Reinit snapshot or transactional subscription (article level)
  11913.         if @publication_type = 0 or @publication_type = 1
  11914.         begin
  11915.             set @proc = @publisher + '.' + @publisher_db + '.dbo.sp_reinitsubscription'
  11916.             exec @retcode = @proc
  11917.                 @publication = @publication,
  11918.                 @article = @article,
  11919.                 @subscriber = @subscriber,
  11920.                 @destination_db = @subscriber_db
  11921.             -- Ignore failures, update status bit if successful
  11922.             if @retcode = 0
  11923.             begin
  11924.                 -- Change status to 1 which means the alerts has been serviced
  11925.                 update msdb..sysreplicationalerts set status = 1 where alert_id = @alert_id
  11926.  
  11927.                 -- Raiserror that subscription was reinitialized
  11928.                 if @failure_level = 0
  11929.                     -- 'Subscriber ''%s'' subscription to article ''%s'' in publication ''%s'' has been reinitialized after a synchronization failure.'
  11930.                     raiserror(20576, 10,-1, @subscriber, @article, @publication) 
  11931.                 else if @failure_level  = 1
  11932.                     -- 'Subscriber ''%s'' subscription to article ''%s'' in publication ''%s'' has been reinitialized after a validation failure.'
  11933.                     raiserror(20572, 10,-1, @subscriber, @article, @publication) 
  11934.             end
  11935.             else -- failure
  11936.                 set @return_value = 1
  11937.         end
  11938.         -- Reinit merge subscription (full publication)
  11939.         else if @publication_type = 2       
  11940.         begin
  11941.             set @proc = @publisher + '.' + @publisher_db + '.dbo.sp_reinitmergesubscription'
  11942.             exec @retcode = @proc
  11943.                 @publication = @publication,
  11944.                 @subscriber = @subscriber,
  11945.                 @subscriber_db = @subscriber_db
  11946.             -- Ignore failures, update status bit if successful
  11947.             if @retcode = 0
  11948.             begin
  11949.                 -- Change status to 1 which means the alerts has been serviced
  11950.                 update msdb..sysreplicationalerts set status = 1 where alert_id = @alert_id
  11951.  
  11952.                 -- Raiserror that subscription was reinitialized
  11953.                 if @failure_level = 0
  11954.                     -- 'Subscriber ''%s'' subscription to to article ''%s'' in publication ''%s'' has been reinitialized after a synchronization failure.'
  11955.                      raiserror(20576, 10,-1, @subscriber, @article, @publication) 
  11956.                 else if @failure_level  = 1
  11957.                     -- 'Subscriber ''%s'' subscription to to article ''%s'' in publication ''%s'' has been reinitialized after a validation failure.'
  11958.                     raiserror(20572, 10,-1, @subscriber, @article, @publication)
  11959.             end
  11960.             else -- failure
  11961.                 set @return_value = 1
  11962.         end
  11963.  
  11964.         fetch hc into  @publisher, @publisher_db, @publication, @publication_type, @article, @subscriber, @subscriber_db, @alert_id
  11965.     end
  11966.  
  11967.     close hc
  11968.     deallocate hc
  11969.  
  11970.     -- Return a message stating no entries where found
  11971.     if @found = 0
  11972.     begin
  11973.         -- 'No entries were found in msdb..sysreplicationalerts.'
  11974.         raiserror(20577, 10,-1) 
  11975.  
  11976.         -- There is most likely a problem, set failure return value
  11977.         set @return_value = 1
  11978.     end
  11979.  
  11980.     return @return_value 
  11981. go
  11982. dump tran master with no_log
  11983. GO
  11984.  
  11985. raiserror('Creating procedure sp_add_datatype_mapping', 0,1)
  11986. go
  11987.  
  11988. -- Add a row into the "MSdatatype_mappings" table
  11989. create procedure sp_add_datatype_mapping (
  11990.     @dbms_name          sysname,
  11991.     @sql_type           sysname,
  11992.     @dest_type          sysname,
  11993.     @dest_prec          int,
  11994.     @dest_create_params int,
  11995.     @dest_nullable      bit
  11996. )
  11997. as
  11998.     set nocount on
  11999.  
  12000.     if @dbms_name is null
  12001.     BEGIN
  12002.         RAISERROR (14043, 16, -1, '@dbms_name')
  12003.         RETURN (1)
  12004.     END
  12005.     
  12006.     if @sql_type is null 
  12007.     BEGIN
  12008.         RAISERROR (14043, 16, -1, '@sql_type')
  12009.         RETURN (1)
  12010.     END
  12011.  
  12012.     if @dest_type is null
  12013.     BEGIN
  12014.         RAISERROR (14043, 16, -1, '@dest_type')
  12015.         RETURN (1)
  12016.     END
  12017.  
  12018.     if @dest_prec is null
  12019.     BEGIN
  12020.         RAISERROR (14043, 16, -1, '@dest_prec')
  12021.         RETURN (1)
  12022.     END
  12023.  
  12024.     if @dest_create_params is null
  12025.     BEGIN
  12026.         RAISERROR (14043, 16, -1, '@dest_create_params')
  12027.         RETURN (1)
  12028.     END
  12029.  
  12030.     if @dest_nullable is null
  12031.     BEGIN
  12032.         RAISERROR (14043, 16, -1, '@dest_nullable')
  12033.         RETURN (1)
  12034.     END
  12035.  
  12036.  
  12037.     insert into msdb..MSdatatype_mappings
  12038.     values (@dbms_name, @sql_type, @dest_type, @dest_prec, @dest_create_params, @dest_nullable)
  12039.  
  12040.     if @@error <> 0
  12041.         return(1)
  12042. GO
  12043.  
  12044. raiserror('Creating procedure sp_MSrepl_gettype_mappings', 0,1)
  12045. go
  12046. create procedure sp_MSrepl_gettype_mappings(
  12047.     @dbms_name          sysname,
  12048.     @sql_type           sysname = '%',
  12049.     @source_prec        int = NULL
  12050. )
  12051. as
  12052.     set nocount on
  12053.     declare @retcode int
  12054.     declare @distributor sysname
  12055.     declare @distribdb sysname
  12056.     declare @distproc nvarchar (255)
  12057.     
  12058.     EXECUTE @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT,
  12059.            @distribdb   = @distribdb OUTPUT
  12060.     IF @@ERROR <> 0 or @retcode <> 0
  12061.     BEGIN
  12062.         RAISERROR (14071, 16, -1)
  12063.         RETURN (1)
  12064.     END
  12065.  
  12066.     SELECT @distproc = RTRIM(@distributor) + '.master' + 
  12067.             '.dbo.sp_help_datatype_mapping'
  12068.  
  12069.     EXECUTE @retcode = @distproc
  12070.                        @dbms_name = @dbms_name,
  12071.                        @sql_type = @sql_type,
  12072.                        @source_prec = @source_prec
  12073.             
  12074.     IF @@ERROR <> 0 OR @retcode <> 0
  12075.         return 1
  12076.     ELSE
  12077.         return 0
  12078. go
  12079.  
  12080. raiserror('Creating procedure sp_help_datatype_mapping', 0,1)
  12081. go
  12082. create procedure sp_help_datatype_mapping(
  12083.     @dbms_name          sysname,
  12084.     @sql_type           sysname = '%',
  12085.     @source_prec        int = NULL
  12086. )
  12087. as
  12088.     set nocount on
  12089.     
  12090.     if @source_prec is NULL
  12091.     begin
  12092.         select sql_type, dest_type, dest_prec, dest_create_params, dest_nullable
  12093.         from msdb..MSdatatype_mappings 
  12094.         where dbms_name = @dbms_name collate database_default
  12095.         and sql_type like @sql_type collate database_default
  12096.     end
  12097.     else
  12098.     begin
  12099.         select sql_type, dest_type, dest_prec, dest_create_params, dest_nullable
  12100.         from msdb..MSdatatype_mappings 
  12101.         where dbms_name = @dbms_name collate database_default
  12102.         and sql_type like @sql_type collate database_default
  12103.         and dest_prec = (select min(dest_prec)
  12104.                             from msdb..MSdatatype_mappings 
  12105.                             where dbms_name = @dbms_name collate database_default
  12106.                             and sql_type like @sql_type collate database_default
  12107.                             and dest_prec >= @source_prec
  12108.                         )
  12109.     end
  12110. GO
  12111.  
  12112. raiserror('Creating procedure sp_MSfix_6x_tasks', 0,1)
  12113. go
  12114. CREATE PROCEDURE sp_MSfix_6x_tasks (
  12115.     @publisher sysname = NULL
  12116. ) AS
  12117.  
  12118.     SET NOCOUNT ON
  12119.  
  12120.     DECLARE @distributor sysname
  12121.     DECLARE @distproc nvarchar (255)
  12122.     declare @retcode int
  12123.  
  12124.     -- If @publisher is null redirect the call to distributor
  12125.     if @publisher is null
  12126.     begin
  12127.         /*
  12128.         ** Get distribution server information for remote RPC
  12129.         ** agent verification.
  12130.         */
  12131.         EXEC @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor OUTPUT
  12132.  
  12133.         IF @@error <> 0 OR @retcode <> 0
  12134.         BEGIN
  12135.             RAISERROR (14071, 16, -1)
  12136.             RETURN (1)
  12137.         END
  12138.  
  12139.         /*
  12140.         ** RPC distributor
  12141.         */
  12142.         SELECT @distproc = RTRIM(@distributor) + '.master.dbo.sp_MSfix_6x_tasks'
  12143.  
  12144.         EXECUTE @retcode = @distproc
  12145.             @publisher = @@SERVERNAME
  12146.         IF @@ERROR <> 0 or @retcode <> 0
  12147.             return (1)
  12148.     end
  12149.     else
  12150.     begin
  12151.  
  12152.         declare @category_id int
  12153.         declare @category_name sysname
  12154.         declare @server sysname
  12155.         declare @databasename sysname
  12156.         declare @name sysname
  12157.         declare @distdb sysname
  12158.         declare @job_id uniqueidentifier
  12159.         declare @sSubsystem sysname
  12160.  
  12161.         -- Drop entry in systasks first.
  12162.         DECLARE hCtasks CURSOR LOCAL FAST_FORWARD FOR
  12163.             SELECT name FROM msdb.dbo.systasks_view st WHERE 
  12164.                 
  12165.                 -- drop distribution agents.
  12166.                 (st.name LIKE @publisher + '_' + '%' + '_' + '%' AND 
  12167.                 LOWER(subsystem collate SQL_Latin1_General_CP1_CS_AS) = 'distribution' AND
  12168.                 server = @@SERVERNAME) OR
  12169.                                 
  12170.                 (LOWER(subsystem collate SQL_Latin1_General_CP1_CS_AS) = 'logreader' AND
  12171.                 server = @publisher) OR 
  12172.                 
  12173.                 (LOWER(subsystem collate SQL_Latin1_General_CP1_CS_AS) = 'snapshot' AND
  12174.                 server = @publisher) OR
  12175.  
  12176.                 (st.name LIKE '%' + '_' + '%' + '_Cleanup' AND 
  12177.                 st.command LIKE '%' + 'sp_replcleanup' + '%' AND
  12178.                 LOWER(subsystem collate SQL_Latin1_General_CP1_CS_AS) = 'tsql')
  12179.             FOR READ ONLY
  12180.  
  12181.         OPEN hCtasks
  12182.         FETCH hCtasks INTO @name
  12183.  
  12184.         WHILE (@@fetch_status <> -1)
  12185.         BEGIN
  12186.             
  12187.             EXEC @retcode = msdb.dbo.sp_droptask @name = @name
  12188.             if @retcode <> 0 or @@error <> 0
  12189.                 return(1)
  12190.             FETCH hCtasks INTO @name
  12191.         END
  12192.  
  12193.         -- Now spin through each old replication job and fixup categories names
  12194.         declare hcJobsToFix CURSOR LOCAL FAST_FORWARD for 
  12195.                 select distinct j.job_id, j.name, s.subsystem, 
  12196.                     s.server, s.database_name
  12197.                     from msdb.dbo.sysjobs j left join msdb.dbo.sysjobsteps s
  12198.                         on j.job_id = s.job_id
  12199.                     where j.category_id = 0 and s.step_id = 1 and lower(s.subsystem collate SQL_Latin1_General_CP1_CS_AS) in ( 'snapshot', 'logreader', 'distribution' )
  12200.                 for read only
  12201.  
  12202.         open hcJobsToFix 
  12203.         fetch hcJobsToFix into @job_id, @name, @sSubsystem,
  12204.             @server,@databasename
  12205.  
  12206.         while (@@fetch_status <> -1 )
  12207.         begin
  12208.             -- Note, have to make it a transaction, once the category_id is changed,
  12209.             -- the task will never be picked up again.
  12210.             begin tran 
  12211.  
  12212.                 -- Get Distribution category name (assumes category_id = 10)
  12213.                 select @category_id =
  12214.                     case lower(@sSubsystem collate SQL_Latin1_General_CP1_CS_AS) 
  12215.                         when 'snapshot' then 15
  12216.                         when 'distribution' then 10
  12217.                         when 'logreader' then 13
  12218.                         else 0
  12219.                     end
  12220.  
  12221.                 select @category_name = name FROM msdb.dbo.syscategories where category_id = @category_id
  12222.                 
  12223.                 -- raiserror( 'Would update %s to category %d based on subsystem value %s', -1, 10, @nJobName, @iCategory, @sSubsystem )
  12224.                 exec @retcode = msdb.dbo.sp_update_job @job_id = @job_id, 
  12225.                     @category_name = @category_name
  12226.                 if @retcode <> 0 or @@error <> 0
  12227.                     goto UNDO
  12228.  
  12229.  
  12230.                 -- Add the replication agent for monitoring
  12231.                 SELECT @distdb = distribution_db from msdb..MSdistpublishers where UPPER(name) = UPPER(@server) collate database_default
  12232.                 IF (@category_id = 13) -- Logreader
  12233.                 BEGIN
  12234.                     SELECT @distproc = @distdb + '.dbo.sp_MSadd_logreader_agent'
  12235.                     EXECUTE @retcode = @distproc
  12236.                         @name = @name,
  12237.                         @publisher = @server,
  12238.                         @publisher_db = @databasename,
  12239.                         @publication = '',  
  12240.                         @local_job = 1,
  12241.                         @job_existing = 1,
  12242.                         @job_id = @job_id
  12243.  
  12244.                     IF (@retcode <> 0 or @@error<>0)
  12245.                         goto UNDO
  12246.                 END
  12247.                 ELSE 
  12248.                 IF (@category_id = 15) -- Snapshot
  12249.                 BEGIN
  12250.                     DECLARE @publication sysname
  12251.  
  12252.                     SELECT @publication = NULL
  12253.                     EXECUTE master.dbo.sp_MSget_publication_from_taskname
  12254.                                             @taskname = @name,
  12255.                                             @publisher = @server,
  12256.                                             @publisherdb = @databasename,
  12257.                                             @publication = @publication OUTPUT
  12258.  
  12259.                     IF (@publication IS NOT NULL )
  12260.                     BEGIN                
  12261.                         SELECT @distproc = @distdb + '.dbo.sp_MSadd_publication'
  12262.                         EXECUTE @retcode = @distproc
  12263.                             @publisher = @server,
  12264.                             @publisher_db = @databasename,
  12265.                             @publication = @publication,
  12266.                             @publication_type = 0 -- Transactional
  12267.                         IF (@retcode <> 0 or @@error<>0)
  12268.                             goto UNDO
  12269.  
  12270.                         SELECT @distproc = @distdb + '.dbo.sp_MSadd_snapshot_agent'
  12271.                         EXECUTE @retcode = @distproc
  12272.                             @name = @name,
  12273.                             @publisher = @server,
  12274.                             @publisher_db = @databasename,
  12275.                             @publication = @publication,
  12276.                             @local_job = 1,
  12277.                             @job_existing = 1,
  12278.                             @snapshot_jobid = @job_id
  12279.                         IF (@retcode <> 0 or @@error<>0)
  12280.                             goto UNDO
  12281.                     END
  12282.                 END
  12283.             commit tran
  12284.             fetch hcJobsToFix into @job_id, @name, @sSubsystem,
  12285.                 @server,@databasename
  12286.         end            
  12287.         close hcJobsToFix
  12288.         deallocate hcJobsToFix
  12289.     end
  12290.     return (0)
  12291. UNDO:
  12292.     if @@trancount <> 0
  12293.         rollback tran
  12294.     return (1)
  12295. GO
  12296.  
  12297. /*
  12298.  * Name :       sp_MShelpconflictpublications
  12299.  * Description: This sp returns a list of publications or subscriptions in the current
  12300.  *    database that may have conflicts. Results are ordered by publication
  12301.  *    name.
  12302.  * Parameters:  1. publication_type( sysname; '%'==ALL (default) | 'merge' | 'queued' )
  12303.  * Output Result Set has the following structure:
  12304.  *  ----------------------------------------------------------------------------------
  12305.  *      Name                Datatype                Description
  12306.  *  ----------------------------------------------------------------------------------
  12307.  *  a. name                    (sysname)            Publication name
  12308.  *  b. publication_type        (varchar(9))        'merge' | 'queued' | 'immediate' (reserved)
  12309.  *  c. merge_pub_id            (uniqueidentifier)    Merge publication identifier
  12310.  *  d. queued_pub_id        (integer)            Queued publication identifier (I would prefer to name this as tran_pub_id)
  12311.  *  e. sub_agent_id            (integer)            Unique publication identifier on a tran subscriber
  12312.  *  NOTE: In case of queued tran publications, either d or e will have a value at any time
  12313.  *  and this will also indicate, if we are processing a subscriber of a publication (d 
  12314.  *  will be NULL and e will have a value) or if we are processing the publisher side (d
  12315.  *  will have a value and e will be NULL)
  12316. */
  12317. raiserror('Creating procedure sp_MShelpconflictpublications', 0,1)
  12318. GO
  12319.  
  12320. CREATE PROCEDURE sp_MShelpconflictpublications ( @publication_type varchar(9) ='%' )
  12321. AS 
  12322. BEGIN
  12323.  
  12324.     SET nocount ON
  12325.     
  12326.     -- validate publication type
  12327.     IF NOT( lower(@publication_type collate SQL_Latin1_General_CP1_CS_AS) IN ('%', 'merge', 'queued') )
  12328.     BEGIN
  12329.         raiserror( 'invalid publication type', 16, -1 )
  12330.         RETURN (1)
  12331.     END
  12332.     
  12333.     -- temp table to store combined result set
  12334.     CREATE TABLE #result_list ( publication sysname collate database_default, publication_type VARCHAR(9) DEFAULT 'merge', 
  12335.                                 merge_pub_id UNIQUEIDENTIFIER NULL, 
  12336.                                 queued_pub_id INTEGER NULL, sub_agent_id INTEGER NULL, publisher sysname collate database_default NULL, publisher_db sysname collate database_default NULL)
  12337.  
  12338.     -- fetch merge results into temp table; need not affect any rows
  12339.     IF ( lower(@publication_type collate SQL_Latin1_General_CP1_CS_AS) IN ('%', 'merge') )
  12340.     BEGIN
  12341.         IF EXISTS( SELECT * FROM sysobjects WHERE name = 'sysmergepublications' )
  12342.             INSERT #result_list ( publication, merge_pub_id, publisher, publisher_db)
  12343.                 EXEC sp_MShelpmergeconflictpublications
  12344.  
  12345.         -- may return 18757 (not merge published) and that is ok if @publication_type = ALL
  12346.         IF ( @@ERROR <> 0) AND ( @@ERROR = 18757 AND @publication_type = '%' ) 
  12347.             GOTO FAILURE
  12348.                 
  12349.     END
  12350.  
  12351.     -- fetch tran results into temp table
  12352.     IF ( lower(@publication_type collate SQL_Latin1_General_CP1_CS_AS) IN ('%', 'queued') )
  12353.     BEGIN
  12354.         INSERT #result_list  ( publication, publication_type, merge_pub_id , queued_pub_id, sub_agent_id )
  12355.             EXEC sp_MShelptranconflictpublications
  12356.         IF ( @@ERROR <> 0) 
  12357.             GOTO FAILURE
  12358.  
  12359.     END
  12360.  
  12361.     -- return combined result list
  12362.     SELECT * FROM #result_list
  12363.  
  12364.     IF EXISTS( SELECT * FROM sysobjects WHERE type = 'U' AND name = '#result_list' )
  12365.         DROP TABLE #result_list
  12366.  
  12367.     RETURN (0)
  12368.  
  12369. FAILURE:
  12370.  
  12371.     IF EXISTS( SELECT * FROM sysobjects WHERE type = 'U' AND name = '#result_list' )
  12372.         DROP TABLE #result_list
  12373.  
  12374.     RETURN (1)
  12375.     
  12376. END
  12377. GO
  12378.  
  12379. raiserror('Creating function system_function_schema.fn_replgetagentcommandlinefromjobid',0,1)
  12380. go
  12381. --
  12382. -- Name: fn_replgetagentcommandlinefromjobid
  12383. --
  12384. -- Description: This function retrieves the command line  
  12385. --              of the replication agent with the 
  12386. --              specified job_id.
  12387. --
  12388. -- Parameters: @agenttype nvarchar(16)
  12389. --             @job_id uniqueidentifier
  12390. -- 
  12391. -- Returns: nvarchar(3200) 
  12392. --
  12393. -- Notes: This function can return null if the 
  12394. --        the given job_id doesn't belong to a
  12395. --        replication agent of the specified type.
  12396. --
  12397. -- Security: Admin access only
  12398. -- 
  12399. create function system_function_schema.fn_replgetagentcommandlinefromjobid (
  12400.     @agenttype nvarchar(16),
  12401.     @job_id uniqueidentifier
  12402.     ) returns nvarchar(3200)
  12403. as
  12404. begin
  12405.     declare @commandline nvarchar(3200)
  12406.     select @commandline = null
  12407.     select @agenttype = upper(@agenttype collate SQL_Latin1_General_CP1_CS_AS)
  12408.     select @commandline = command 
  12409.       from msdb.dbo.sysjobsteps
  12410.      where job_id = @job_id
  12411.        and upper(subsystem collate SQL_Latin1_General_CP1_CS_AS) = @agenttype collate SQL_Latin1_General_CP1_CS_AS
  12412.  
  12413.     return @commandline
  12414. end
  12415. go
  12416.  
  12417. raiserror('Creating procedure sp_replproberemoteserver', 0,1)
  12418. go
  12419. --
  12420. -- Name: sp_replproberemoteserver
  12421. --
  12422. -- Description: This is a lightweight wrapper for calling xp_replproberremsrv
  12423. --              with the added nicety of looking up the agent command line
  12424. --              using the given jobid. This procedure supports two different
  12425. --              modes of operation based on the @no_rpc parameter. If the 
  12426. --              @no_rpc parameter is 1, this procedure will not attempt to make
  12427. --              rpc call to the Distributor. The local mode is mainly useful 
  12428. --              for verifying remote pull subscription agent.
  12429. -- 
  12430. -- Parameters: @remoteservername sysname (mandatory) 
  12431. --             @agent_type nvarchar(16) (optional, default = null) 
  12432. --             @agent_jobid uniqueidentifier (optional default = null)
  12433. --             @no_rpc bit (optional default = 0)
  12434. --
  12435. -- Notes: If @job_id is null, only activation verification will be carried
  12436. --        out.
  12437. --
  12438. -- Security: Execute permission of this procedure is granted to public
  12439. --
  12440. -- Result: 'probe_succeeded' 0 or 1
  12441. --
  12442. -- Returns: 0    - succeeded
  12443. --          <> 0 - failed
  12444. --
  12445. create procedure dbo.sp_replproberemoteserver (
  12446.     @remoteservername sysname,
  12447.     @agent_type       nvarchar(16) = null,
  12448.     @agent_jobid      uniqueidentifier = null,
  12449.     @no_rpc           bit = 0
  12450.     ) 
  12451. as
  12452. begin
  12453.     set nocount on
  12454.     declare @retcode        int
  12455.     declare @commandline    nvarchar(3200)
  12456.     declare @distributor    sysname
  12457.     declare @distributiondb sysname     
  12458.     declare @distproc       nvarchar(255)
  12459.     declare @succeeded      nvarchar(10)
  12460.  
  12461.     select @retcode = 0
  12462.     select @commandline = null
  12463.     select @succeeded = null
  12464.  
  12465.     if @agent_type is null
  12466.     begin
  12467.         select @agent_type = 'distribution'
  12468.     end
  12469.     
  12470.     -- @remoteservername cannot be null or empty
  12471.     select @remoteservername = rtrim(ltrim(@remoteservername))
  12472.     if @remoteservername is null or
  12473.        @remoteservername = N''
  12474.     begin
  12475.         raiserror(21263,16,-1,'@remoteservername')
  12476.         return 1
  12477.     end     
  12478.     
  12479.     -- @agent_type must be 'distribution' or 'merge'
  12480.     if @agent_type not in (N'distribution', N'merge')
  12481.     begin
  12482.         raiserror(21182,16,-1) 
  12483.         return 1
  12484.     end
  12485.  
  12486.     -- Get Distributor information
  12487.     if @no_rpc = 0
  12488.     begin
  12489.         exec @retcode = dbo.sp_helpdistributor @rpcsrvname = @distributor output,
  12490.                                           @distribdb = @distributiondb output
  12491.         if @@error <> 0 or @retcode <> 0
  12492.             return 1
  12493.     end
  12494.     else
  12495.     begin
  12496.         select @distributor = @@servername
  12497.     end
  12498.  
  12499.     if upper(@@servername) <> upper(@distributor)
  12500.     begin
  12501.         select @distproc = ltrim(rtrim(@distributor)) + '.' + 
  12502.             'master.dbo.sp_replproberemoteserver'
  12503.         exec @retcode = @distproc @remoteservername, 
  12504.                                   @agent_type, 
  12505.                                   @agent_jobid, 
  12506.                                   1
  12507.         return @retcode
  12508.     end        
  12509.     else
  12510.     begin
  12511.         -- If the given @job_id is not null, try to get
  12512.         -- the agent command line
  12513.         if @agent_jobid is not null 
  12514.         begin
  12515.             select @commandline = fn_replgetagentcommandlinefromjobid(
  12516.                 @agent_type,
  12517.                 @agent_jobid) collate database_default
  12518.  
  12519.             if @commandline is null
  12520.             begin
  12521.                 raiserror(21361,6,-1, @agent_type) 
  12522.                 select 'probe_succeeded' = 0
  12523.                 return 1
  12524.             end
  12525.  
  12526.         end 
  12527.  
  12528.         exec @retcode = master.dbo.xp_replproberemsrv 
  12529.             @remoteservername, 
  12530.             @agent_type, 
  12531.             @succeeded output,
  12532.             @commandline 
  12533.  
  12534.         if lower(@succeeded collate SQL_Latin1_General_CP1_CS_AS) = N'true'  
  12535.             select 'probe_succeeded' = 1
  12536.         else
  12537.             select 'probe_succeeded' = 0
  12538.  
  12539.         return @retcode
  12540.     end
  12541.  
  12542.     select 'probe_succeeded' = 0
  12543.  
  12544.     return 1
  12545. end
  12546. go
  12547. exec sp_MS_marksystemobject sp_replproberemoteserver
  12548. go
  12549. grant execute on dbo.sp_replproberemoteserver to public
  12550. go
  12551.  
  12552. raiserror('Creating procedure sp_MScleanupmergepublisher', 0,1)
  12553. go
  12554. --
  12555. -- Name: sp_MScleanupmergepublisher
  12556. -- 
  12557. -- Description: This procedure currently performs the following function(s):
  12558. --              1) Cleans up all the stale dynamic snapshot views
  12559. --              in all databases enabled for merge replication. This 
  12560. --              procedure should normally be called at merge publisher startup.
  12561. --              
  12562. -- Notes: 1)This procedure is enabled as a startup procedure when a database is
  12563. --        enabled as a first merge publisher database on the server and it 
  12564. --        will be unmarked as a startup procedure when the last merge publisher
  12565. --        database is disabled.
  12566. --        2)Errors within the SP are mostly ignored.
  12567. --        3)This procedure can also be used by admins/securityadmins to perform
  12568. --        manual cleanup of all dynamic snapshot views. Note that cleaning up the 
  12569. --        dynamic snapshot views can disrupt dynamic snapshots that are being generated.
  12570. --        
  12571. -- Returns: (undefined)
  12572. --
  12573. -- Security: Only members of the sysadmin fixed server role can execute this
  12574. --           procedure successfully. So for this procedure to function proprely 
  12575. --           as a startup procedure, the MSSQLServer service account must be a 
  12576. --           member of the sysadmin role.
  12577. --
  12578. create procedure sp_MScleanupmergepublisher
  12579. as
  12580. begin
  12581.     set nocount on
  12582.     declare @status_mask int
  12583.     declare @published_mask int
  12584.     declare @published_database_name sysname
  12585.     declare @command nvarchar(4000)
  12586.  
  12587.     -- Masks off the databases with status that we don't want to deal with
  12588.     select @status_mask = 32 | -- loading
  12589.                           64 | -- pre recovery
  12590.                           128 | -- recovering
  12591.                           256 | -- not recovered
  12592.                           512 | -- offline
  12593.                           1024  -- read only
  12594.  
  12595.     select @published_mask = 4 -- Merge published
  12596.  
  12597.     declare hPublishedDatabase cursor local fast_forward for
  12598.         select name from sysdatabases 
  12599.          where (status & @status_mask) = 0
  12600.            and (category & @published_mask) <> 0
  12601.  
  12602.     open hPublishedDatabase
  12603.  
  12604.     fetch hPublishedDatabase into @published_database_name
  12605.     while  (@@fetch_status <> -1)
  12606.     begin
  12607.  
  12608.         select @command = 'use ' + QUOTENAME(@published_database_name) + '
  12609. ' +                       'exec sp_MScleanupmergepublisherdb'
  12610.  
  12611.  
  12612.         exec (@command)
  12613.         -- Ignore errors
  12614.         fetch hPublishedDatabase into @published_database_name
  12615.     end
  12616.  
  12617.     close hPublishedDatabase
  12618.     deallocate hPublishedDatabase
  12619.  
  12620. end
  12621. go
  12622. exec sp_MS_marksystemobject sp_MScleanupmergepublisher
  12623. go
  12624.  
  12625. --
  12626. -- Name: sp_MScleanupdynsnapshotvws
  12627. --
  12628. -- Description: This procedure is used to drop all the views listed in the     
  12629. --              MSdynamicsnapshotviews table of the publisher database. It is 
  12630. --              typically called by the merge cleanup procedure 
  12631. --              (sp_MScleanupmergepublisherdb) during server startup although
  12632. --              the database administrator can choose to proactively call this
  12633. --              procedure manually to clean up any temporary dynamic snapshot
  12634. --              views left over from an abnormally terminated dynamic snapshot
  12635. --              generation session.
  12636. --
  12637. -- Parameter: (none) 
  12638. --                 
  12639. -- Notes: Calling this procedure while a dynamic snapshot is being generated 
  12640. --        will probably disrupt the said dynamic snapshot generation session.
  12641. -- 
  12642. -- Returns: 0 - succeeded
  12643. --          1 - failed
  12644. -- 
  12645. -- Result: none
  12646. --
  12647. -- Security: Execute permission of this procedure is granted to public. 
  12648. --           Internally, this procedure will call sp_MSreplcheck_publish
  12649. --           to make sure that the caller is either server sysadmin or dbo 
  12650. --           of the publishing database
  12651. --              
  12652. raiserror('Creating procedure sp_MScleanupdynsnapshotvws', 0,1)
  12653. go
  12654. create procedure sp_MScleanupdynsnapshotvws
  12655. as
  12656. begin
  12657.     set nocount on    
  12658.     declare @retcode int
  12659.     select @retcode = 0
  12660.     
  12661.     -- Security check
  12662.     exec @retcode = sp_MSreplcheck_publish
  12663.     if @@error<>0 or @retcode<>0
  12664.     begin
  12665.         return 1
  12666.     end
  12667.  
  12668.     -- Do nothing if the MSdynamicsnapshotviews table does not exist
  12669.     if exists (select * from sysobjects where name = N'MSdynamicsnapshotviews')
  12670.     begin
  12671.         declare @dynamic_snapshot_view_name sysname
  12672.         declare @drop_view_command nvarchar(4000) 
  12673.  
  12674.         declare hViews cursor local fast_forward for
  12675.          select dynamic_snapshot_view_name
  12676.            from MSdynamicsnapshotviews
  12677.         if @@error<>0
  12678.         begin
  12679.             goto Failure
  12680.         end
  12681.         
  12682.         open hViews
  12683.         if @@error<>0
  12684.         begin
  12685.            goto Failure
  12686.         end
  12687.     
  12688.         fetch hViews into @dynamic_snapshot_view_name
  12689.         while (@@fetch_status<>-1)
  12690.         begin
  12691.             select @drop_view_command = 'drop view ' + 
  12692.                 quotename(@dynamic_snapshot_view_name)
  12693.             exec(@drop_view_command)
  12694.             if @@error<>0
  12695.             begin
  12696.                 select @retcode = 1 -- Just to indicate that a failure occurred
  12697.             end        
  12698.             delete from MSdynamicsnapshotviews
  12699.              where dynamic_snapshot_view_name = @dynamic_snapshot_view_name
  12700.             if @@error<>0
  12701.             begin
  12702.                 select @retcode = 1 -- Just to indicate that a failure occurred
  12703.             end        
  12704.             fetch hViews into @dynamic_snapshot_view_name
  12705.         end
  12706.     end
  12707.     return @retcode
  12708. Failure:
  12709.     return 1
  12710. end
  12711. go
  12712. exec sp_MS_marksystemobject 'sp_MScleanupdynsnapshotvws'
  12713. go
  12714. raiserror('Creating procedure sp_MScleanupmergepublisherdb',0,1)
  12715. go
  12716. --
  12717. -- Name: sp_MScleanupmergepublisherdb
  12718. --
  12719. -- Description: This procedure is the per-database analogue of 
  12720. --              sp_MScleanupmergepublisher and it currently performs the 
  12721. --              following function(s):
  12722. --              1) Cleans up all the stale dynamic snapshot views
  12723. --              in the current database.
  12724. --
  12725. -- Returns: (undefined)
  12726. --
  12727. -- Security: Only members of the sysadmin fixed server role can execute this
  12728. --           procedure.
  12729. --
  12730. create procedure sp_MScleanupmergepublisherdb
  12731. as
  12732. begin
  12733.     set nocount on
  12734.     declare @temp_login sysname
  12735.  
  12736.     if exists (select * from sysobjects
  12737.         where name = 'MSdynamicsnapshotviews')
  12738.     begin
  12739.         -- Ignore errors
  12740.         exec sp_MScleanupdynsnapshotvws
  12741.     end
  12742.  
  12743. end
  12744. go
  12745. exec sp_MS_marksystemobject sp_MScleanupmergepublisherdb 
  12746. go
  12747.  
  12748. raiserror('Creating procedure sp_MShelp_replication_table',0,1)
  12749. go
  12750. --
  12751. -- Name: sp_MShelp_replication_table
  12752. --
  12753. -- Description: This procedure is used by DMO to get tables can be published and
  12754. --                their properties
  12755. --
  12756. -- Returns: error code
  12757. --
  12758. -- Security: public
  12759. --
  12760. CREATE PROCEDURE sp_MShelp_replication_table (
  12761.     @table_name sysname = NULL,
  12762.     @table_owner sysname = NULL
  12763.     ) 
  12764. AS
  12765.  
  12766. SET NOCOUNT ON
  12767.  
  12768. declare @objid int
  12769.  
  12770. if @table_name is not null
  12771. begin
  12772.     if @table_owner is NULL
  12773.         select @table_owner = user_name()
  12774.     declare @qualified_table_name    nvarchar(260)
  12775.     select @qualified_table_name = QUOTENAME(@table_owner) + '.' + QUOTENAME(@table_name)
  12776.     select @objid = object_id(@qualified_table_name)
  12777.     if @objid is null
  12778.     begin
  12779.         RAISERROR (14027, 11, -1, @qualified_table_name)
  12780.         return(1)
  12781.     end
  12782. end
  12783.  
  12784. create table #merge_objects (objid int primary key)
  12785.  
  12786. create table #queued_tran_objects (objid int primary key)
  12787.  
  12788. if object_id('sysmergearticles') is not null
  12789.     insert into #merge_objects select distinct objid from sysmergearticles where 
  12790.         objid = @objid or @objid is null
  12791.  
  12792. if object_id('sysarticles') is not null
  12793.     insert into #queued_tran_objects select distinct objid from sysarticles a,
  12794.         syspublications p where 
  12795.         (objid = @objid or @objid is null) and 
  12796.         a.pubid = p.pubid and
  12797.         p.allow_queued_tran = 1
  12798.  
  12799. select    'table name' = o.name, 
  12800.         'table owner' = user_name(o.uid), 
  12801.         'ID' = o.id, 
  12802.         'Category' = o.category,
  12803.         'HasGuidColumn' = case when 
  12804.                 exists (select * from syscolumns c where c.id = o.id and
  12805.                 xtype = (select xtype from systypes where name = 'uniqueidentifier')) 
  12806.             then cast(1 as bit) 
  12807.             else cast(0 as bit)
  12808.             end,
  12809.         'HasTimeStampColumn' = ObjectProperty(o.id, 'TableHasTimestamp'),
  12810.         'HasRowVersionColumn' = case when 
  12811.                 exists (select * from syscolumns c where c.id = o.id and 
  12812.                 name = N'msrepl_tran_version')
  12813.             then cast(1 as bit) 
  12814.             else cast(0 as bit)
  12815.             end,
  12816.         'HasIdentityColumn' = ObjectProperty(o.id, 'TableHasIdentity'),
  12817.         'HasSQLVariantColumn' = case when 
  12818.                 exists (select * from syscolumns c where c.id = o.id and
  12819.                 xtype = (select xtype from systypes where name = 'sql_variant')) 
  12820.             then cast(1 as bit) 
  12821.             else cast(0 as bit)
  12822.             end,
  12823.         'HasBigIntColumn' = case when 
  12824.                 exists (select * from syscolumns c where c.id = o.id and
  12825.                 xtype = (select xtype from systypes where name = 'bigint'))
  12826.             then cast(1 as bit) 
  12827.             else cast(0 as bit)
  12828.             end,
  12829.         'HasBigIntIdentityColumn' = case when 
  12830.                 exists (select * from syscolumns c where c.id = o.id and
  12831.                 ColumnProperty(o.id, c.name, 'IsIdentity') = 1 and
  12832.                 xtype = (select xtype from systypes where name = 'bigint'))
  12833.             then cast(1 as bit) 
  12834.             else cast(0 as bit)
  12835.             end,
  12836.         'MergePublished' = case when exists (select * from #merge_objects m where
  12837.             m.objid = o.id)
  12838.             then cast(1 as bit) 
  12839.             else cast(0 as bit)
  12840.             end,
  12841.         'QueuedTranPublished' = case when exists (select * from #queued_tran_objects m where
  12842.             m.objid = o.id)
  12843.             then cast(1 as bit) 
  12844.             else cast(0 as bit)
  12845.             end,
  12846.         'HasIdentityNotForReplColumn' = case when 
  12847.                 exists (select * from syscolumns c where c.id = o.id and 
  12848.                     ColumnProperty(o.id, c.name, 'IsIdNotForRepl') = 1)
  12849.             then cast(1 as bit) 
  12850.             else cast(0 as bit)
  12851.             end
  12852.  
  12853.         from sysobjects o where o.xtype = 'U' and 
  12854.             ObjectProperty(o.id, 'IsMSShipped') = 0 and
  12855.             (o.id = @objid or @objid is null)
  12856.             order by 1, 2
  12857.             
  12858. drop table #merge_objects
  12859. drop table #queued_tran_objects
  12860. go
  12861. exec sp_MS_marksystemobject sp_MShelp_replication_table
  12862. grant execute on dbo.sp_MShelp_replication_table to public
  12863. go
  12864. create proc sp_MScopyscriptfile (
  12865.     @scriptfile nvarchar(4000), 
  12866.     @cmd nvarchar(4000) OUTPUT
  12867. )
  12868. as
  12869. declare @directory nvarchar(4000)
  12870. declare @filename nvarchar(1024)
  12871. declare @subdirectory nvarchar(1024)
  12872. declare @retcode int
  12873.  
  12874. IF @scriptfile IS NULL
  12875. BEGIN
  12876.     RAISERROR (14043, 16, -1, '@scriptfile')
  12877.     RETURN (1)
  12878. END
  12879. -- Create the directory on distributor to store script.
  12880. exec sp_helpdistributor @directory=@directory OUTPUT
  12881. select @subdirectory = convert(nvarchar(64), GetDate(), 121)
  12882. select @subdirectory = replace(@subdirectory, N'-', N'')
  12883. select @subdirectory = replace(@subdirectory, N' ', N'')
  12884. select @subdirectory = replace(@subdirectory, N':', N'')
  12885. select @subdirectory = replace(@subdirectory, N'.', N'')
  12886. if(right(@directory, 1) = N'\')
  12887.     select @directory = @directory + @subdirectory
  12888. else
  12889.     select @directory = @directory + N'\' + @subdirectory
  12890. select @cmd = N'md "' + @directory + '"'
  12891. exec @retcode = master..xp_cmdshell @cmd, NO_OUTPUT
  12892. if(@retcode <> 0)
  12893. begin
  12894.     raiserror(21330, 16, -1, @cmd)
  12895.     return (1)
  12896. end
  12897.  
  12898. -- Copy script to distributor
  12899. select @cmd = N'copy "' + @scriptfile + N'" "' + @directory + N'"'
  12900. exec @retcode = master..xp_cmdshell @cmd, NO_OUTPUT
  12901. if(@retcode <> 0)
  12902. begin 
  12903.     raiserror(21331, 16, -1, @cmd)
  12904.     return (1)
  12905. end
  12906.  
  12907. -- Prepare command, to be posted to log
  12908. select @filename = right(@scriptfile, charindex(N'\', reverse(@scriptfile)))
  12909. if(left(@filename, 1) = N'\')    
  12910.     select @cmd = @directory + @filename
  12911. else
  12912.     select @cmd = @directory + N'\' + @filename
  12913. go
  12914. exec sp_MS_marksystemobject sp_MScopyscriptfile
  12915. go
  12916. grant execute on dbo.sp_MScopyscriptfile to public
  12917. go
  12918. /*
  12919. ** Add extended stored procedures for replication support.
  12920. */
  12921. sp_addextendedproc  'sp_repldone', 'repldone extended procedure'
  12922. go
  12923.  
  12924. sp_addextendedproc  'sp_repltrans', 'repltrans extended procedure'
  12925. go
  12926.  
  12927. sp_addextendedproc  'sp_replcounters', 'replcounters extended procedure'
  12928. go
  12929.  
  12930. sp_addextendedproc  'sp_replcmds', 'replcmds extended procedure'
  12931. go
  12932.  
  12933. sp_addextendedproc  'sp_replflush', 'replflush extended procedure'
  12934. go
  12935.  
  12936. dump tran master with no_log
  12937. go
  12938.  
  12939. sp_addextendedproc  'sp_replpostcmd', 'replpostcmd extended procedure'
  12940. go
  12941. sp_addextendedproc  'sp_replpostschema', 'replpostschema extended procedure'
  12942. go
  12943.  
  12944. sp_addextendedproc  'sp_replincrementlsn', 'replincrementlsn extended procedure'
  12945. go
  12946.  
  12947. sp_addextendedproc  'sp_replupdateschema', 'replupdateschema extended procedure'
  12948. go
  12949.  
  12950. sp_addextendedproc  'sp_replsetoriginator', 'replsetoriginator extended procedure'
  12951. go
  12952.  
  12953. sp_addextendedproc  'sp_replsetsyncstatus', 'replsetsyncstatus extended procedure'
  12954. go
  12955.  
  12956. sp_addextendedproc  'sp_replpostsyncstatus', 'replpostsyncstatus extended procedure'
  12957. go
  12958.  
  12959. /*
  12960. ** Add xp_enum_dsn extended procedure
  12961. */
  12962. sp_addextendedproc 'xp_dsninfo','xpstar.dll'
  12963. go
  12964. exec dbo.sp_MS_marksystemobject xp_dsninfo
  12965. go
  12966.  
  12967. /*
  12968. ** Add xp_enum_dsn extended procedure
  12969. */
  12970. sp_addextendedproc 'xp_enumdsn','xpstar.dll'
  12971. go
  12972. exec dbo.sp_MS_marksystemobject xp_enumdsn
  12973. go
  12974.  
  12975. /*
  12976. ** Add xp_oledbinfo extended procedure
  12977. */
  12978. sp_addextendedproc 'xp_oledbinfo','xprepl.dll'
  12979. go
  12980. exec dbo.sp_MS_marksystemobject xp_oledbinfo
  12981. go
  12982.  
  12983. /*
  12984. ** Add xp_repl_encrypt extended procedure
  12985. */
  12986. sp_addextendedproc 'xp_repl_encrypt','xprepl.dll'
  12987. go
  12988. exec dbo.sp_MS_marksystemobject xp_repl_encrypt
  12989. go
  12990.  
  12991.  
  12992. /*
  12993. ** Add xp_repl_convert_encrypt extended procedure
  12994. */
  12995. sp_addextendedproc 'xp_repl_convert_encrypt','xprepl.dll'
  12996. go
  12997. exec dbo.sp_MS_marksystemobject xp_repl_convert_encrypt
  12998. go
  12999.  
  13000.  
  13001.  
  13002. /*
  13003. ** Add xp_repl_convert_encrypt extended procedure
  13004. */
  13005. sp_addextendedproc 'xp_repl_help_connect','xprepl.dll'
  13006. go
  13007. exec dbo.sp_MS_marksystemobject xp_repl_help_connect
  13008. go
  13009.  
  13010.  
  13011.  
  13012. /*
  13013. ** Add xp_replproberemsrv extended procedure
  13014. */
  13015. sp_addextendedproc 'xp_replproberemsrv','xprepl.dll'
  13016. go
  13017. exec dbo.sp_MS_marksystemobject xp_replproberemsrv
  13018. go
  13019.  
  13020. dump tran master with no_log
  13021. go
  13022.  
  13023. grant execute on dbo.sp_addpublication_snapshot to public
  13024. go
  13025. grant execute on dbo.sp_MShelpobjectpublications to public
  13026. go
  13027. grant execute on dbo.sp_helpreplicationdb to public
  13028. go
  13029. grant execute on dbo.sp_enumdsn to public
  13030. go
  13031. grant execute on dbo.sp_helpsubscriberinfo to public
  13032. go
  13033. grant execute on dbo.sp_replica to public
  13034. go
  13035. grant execute on dbo.sp_distcounters to public
  13036. go
  13037. grant execute on dbo.sp_helpdistributor to public
  13038. go
  13039. grant execute on dbo.sp_helpdistributiondb to public
  13040. go
  13041. grant execute on dbo.sp_helpdistpublisher to public
  13042. go
  13043. grant execute on dbo.sp_replcounters to public
  13044. go
  13045. grant execute on dbo.sp_MShelp_distdb to public
  13046. go
  13047. grant execute on dbo.sp_grant_publication_access to public
  13048. go
  13049. grant execute on dbo.sp_revoke_publication_access to public
  13050. go
  13051. grant execute on dbo.sp_help_publication_access to public
  13052. go
  13053. grant execute on dbo.sp_check_publication_access to public
  13054. go
  13055. grant execute on dbo.sp_replsetoriginator to public
  13056. go
  13057.  
  13058.  
  13059. GRANT EXECUTE ON dbo.sp_MShelpconflictpublications TO PUBLIC
  13060. GO
  13061.  
  13062. grant execute on dbo.sp_repldone to public
  13063. go
  13064. grant execute on dbo.sp_repltrans to public
  13065. go
  13066. grant execute on dbo.sp_replcounters to public
  13067. go
  13068. grant execute on dbo.sp_replcmds to public
  13069. go
  13070. grant execute on dbo.sp_replpostschema to public
  13071. go
  13072. grant execute on dbo.sp_replincrementlsn to public
  13073. go
  13074. grant execute on dbo.sp_replsetsyncstatus to public
  13075. go
  13076. grant execute on dbo.sp_replpostsyncstatus to public
  13077. go
  13078. grant execute on sp_MSenum_replication_job to public
  13079. go
  13080. grant execute on sp_MSrepl_gettype_mappings to public
  13081. go
  13082. grant execute on dbo.xp_replproberemsrv to public
  13083. grant execute on sp_MShelp_replication_status to public
  13084. grant execute on sp_MSenum_replication_agents to public
  13085. -- proc need to be granted to public for replmonitor role
  13086. grant execute on sp_help_agent_default to public
  13087. grant execute on sp_help_agent_profile to public
  13088. grant execute on sp_help_agent_parameter to public
  13089. grant execute on sp_helpdistributor_properties to public
  13090. grant execute on sp_MScleanupdynsnapshotvws to public
  13091. grant execute on sp_MSget_agent_names to public
  13092. go
  13093.  
  13094.  
  13095. dump tran master with no_log
  13096. go
  13097.  
  13098.  
  13099.  
  13100. dump tran master with no_log
  13101. go
  13102. sp_configure 'allow updates',0
  13103. go
  13104. reconfigure with override
  13105. go
  13106.  
  13107. print ''
  13108. print 'Checking objects created by replcom.sql.'
  13109. go
  13110. --obsolete   exec dbo.sp_check_objects 'repl'
  13111. exec dbo.sp_MS_upd_sysobj_category 2  --set sysobjects.category | 2 based on crdate.
  13112. go
  13113.  
  13114. print ''
  13115. print 'replcom.sql completed successfully.'
  13116. go
  13117.  
  13118. dump tran master with no_log
  13119. go
  13120. checkpoint
  13121. go
  13122. -- - -----
  13123.